将字符串分割成特定大小的字符串[]

时间:2021-10-27 21:38:23

Assuming my delimiter is ',' (comma), I'm looking for a function split that will guarantee that the returned String[] array is a certain size (no more, no less):

假设我的分隔符是','(逗号),我正在寻找一个函数拆分,它将保证返回的字符串[]数组具有一定的大小(不多,不少):

String[] split(String str, int limit);

split("hello", 2);               => ["hello", ""]
split("hello,", 2);              => ["hello", ""]
split(",hello", 2);              => ["", "hello"]
split("hello,world", 2);         => ["hello", "world"]
split("hello,world,goodbye", 2); => ["hello", "world,goodbye"]

See how the array size is always 2? I can get some of this behavior with Pattern.split, but not for every case... How can I do this?

看到数组的大小总是2吗?我可以用模式来得到这些行为。分开,但不是所有情况……我该怎么做呢?

7 个解决方案

#1


1  

StringUtils.split(string, separator, max) is very close to what you need.

stringutil的。split(字符串、分隔符、max)非常接近您所需要的。

Then you can use ArrayUtils.addAll(result, emptyArray), where emptyArray is an array of size max - result.length.

然后你可以使用ArrayUtils。addAll(结果,emptyArray),其中emptyArray是一个大小为max - result.length的数组。

The functionality you want is too specific, so I doubt there will be anything ready-to-use.

您想要的功能太具体了,所以我怀疑是否会有任何现成的功能。

#2


2  

You can use Arrays.copyOf() to return a padded array. However, the padding will be null instead of an empty string.

可以使用array. copyof()返回填充数组。但是,填充将是空的,而不是空字符串。

String[] parts = Arrays.copyOf("hello,".split(",", 2), 2);
String[] parts1 = Arrays.copyOf(",hello".split(",", 2), 2);
String[] parts2 = Arrays.copyOf("hello,world".split(",", 2), 2);
String[] parts3 = Arrays.copyOf("hello,world,goodbye".split(",", 2), 2);
String[] parts4 = Arrays.copyOf("hello,".split(",", 2), 4); //null padding

#3


0  

I don’t remember all the syntax by heart, but your are looking at something like this:

我不记得所有的语法,但你看到的是这样的:

Pattern p = new Pattern("[^,]+");
String[] strings = new String[limit];
int offset = 0;
int i = 0;
for (; i < limit; i++) {
    Match m = str.match(p, offset);
    if (m == null) break;
    offset += m.string.length + 1;
    strings[i] = m.string;
}
for (; i < limit; i++) {
    strings[i] = "";
}

Again, this is not valid Java

同样,这不是有效的Java

#4


0  

Well, if I understood you correctly you just want to stop splitting as soon as you have a reached the count provided via argument and move the remaining bit wholly into the last field, right? In addition, the limit-parameter should always ensure the fieldcount?

好吧,如果我理解正确的话你只要到达通过参数提供的计数就可以停止分裂剩下的位完全移到最后一个字段中,对吧?此外,限制参数应该始终确保fieldcount?

Wrap a method around the Pattern.split() method which just adds fields until your limit/threshold/minimum is reached.

在Pattern.split()方法周围包装一个方法,该方法只添加字段,直到达到极限/阈值/最小值。

for(i=0;i<limit;++i){
 //add empty fields to array until limit is reached
}

#5


0  

You could do it with a loop that scans the string searching for the delimiter, then put whatever is not ahead of the delimiter in the array, then re-do it until it hits the top array size, that way it just return whatever was ahead as the last entry in the array:

你可以用一个循环扫描搜索的字符串分隔符,然后把任何不是数组中的分隔符,然后重做一遍,直到达到*数组大小,这样它就返回无论前方是数组中的最后一个条目:

public string[] mySplit(String Split, char Delimiter, int Top)
 {
  string[] returned = new string[Top];
  for(int i = 0; i < Top; i++){
    if(i==Top)
    {
     returned[i] = Split; return;
    }
    else
    {
     int compPlace = 0;
     char comp = Split.toCharArray()[compPlace];
     while(comp != Delimiter)
     {
      compPlace++;
      comp = Split.toCharArray()[compPlace];
      }
    returned[i] = Split.SubString(0,compPlace);
    Split = Split.SubString(compPlace);
    }
  }
  return returned;
}

Im sure youll have to tweak this as i have a little headache as of now :)

我敢肯定你得把这个调整一下,因为我现在有点头疼。

#6


0  

It seems that it would be easy enough to pad out a new array with empty strings. Here is a utility method you could use to perform this split.

用空字符串填充一个新的数组似乎很容易。这里有一个实用方法可以用来执行这个分割。

public static String[] split(Pattern pattern, CharSequence input, int limit) {
    // perform the split
    tokens = pattern.split(input, limit);

    int tokenCount = tokens.length();
    if (tokenCount == limit) {
        // if no padding is necessary, return the result of pattern.split
        return tokens;
    } else {
        // create a new array, copy tokens into array, pad with empty string
        String[] padded = Arrays.copyOf(tokens, limit);
        for (int i = tokenCount; i < limit; i++) {
            padded[i] = "";
        }
        return padded;
    }
}

Edit: Updated to shamelessly steal [Arrays.copyOf][1] from unholysampler's answer.

编辑:更新为无耻地偷取[数组]。copyOf][1]从unholysampler的答案。

[1]: http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(T[], int)

[1]:http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html copyOf(T[],int)

#7


0  

Simply getting the position of the delimiter and using substrings gets you there pretty easily:

简单地获取分隔符的位置并使用子字符串就可以很容易地实现:

public static String[] split(String str, int limit){

           String[] arr = new String[limit];

           for(int i = 0; i < limit-1; i++){
               int position = str.indexOf(',');
               int length = str.length();

               if(position == -1){
                   arr[i]= str.substring(0,length);
                   str = str.substring(length, length);
               }
               else{
                   arr[i] = str.substring(0, position);
                   str = str.substring(position+1, length);
               }
            }

            arr[limit-1] = str;

            return arr;
       }

#1


1  

StringUtils.split(string, separator, max) is very close to what you need.

stringutil的。split(字符串、分隔符、max)非常接近您所需要的。

Then you can use ArrayUtils.addAll(result, emptyArray), where emptyArray is an array of size max - result.length.

然后你可以使用ArrayUtils。addAll(结果,emptyArray),其中emptyArray是一个大小为max - result.length的数组。

The functionality you want is too specific, so I doubt there will be anything ready-to-use.

您想要的功能太具体了,所以我怀疑是否会有任何现成的功能。

#2


2  

You can use Arrays.copyOf() to return a padded array. However, the padding will be null instead of an empty string.

可以使用array. copyof()返回填充数组。但是,填充将是空的,而不是空字符串。

String[] parts = Arrays.copyOf("hello,".split(",", 2), 2);
String[] parts1 = Arrays.copyOf(",hello".split(",", 2), 2);
String[] parts2 = Arrays.copyOf("hello,world".split(",", 2), 2);
String[] parts3 = Arrays.copyOf("hello,world,goodbye".split(",", 2), 2);
String[] parts4 = Arrays.copyOf("hello,".split(",", 2), 4); //null padding

#3


0  

I don’t remember all the syntax by heart, but your are looking at something like this:

我不记得所有的语法,但你看到的是这样的:

Pattern p = new Pattern("[^,]+");
String[] strings = new String[limit];
int offset = 0;
int i = 0;
for (; i < limit; i++) {
    Match m = str.match(p, offset);
    if (m == null) break;
    offset += m.string.length + 1;
    strings[i] = m.string;
}
for (; i < limit; i++) {
    strings[i] = "";
}

Again, this is not valid Java

同样,这不是有效的Java

#4


0  

Well, if I understood you correctly you just want to stop splitting as soon as you have a reached the count provided via argument and move the remaining bit wholly into the last field, right? In addition, the limit-parameter should always ensure the fieldcount?

好吧,如果我理解正确的话你只要到达通过参数提供的计数就可以停止分裂剩下的位完全移到最后一个字段中,对吧?此外,限制参数应该始终确保fieldcount?

Wrap a method around the Pattern.split() method which just adds fields until your limit/threshold/minimum is reached.

在Pattern.split()方法周围包装一个方法,该方法只添加字段,直到达到极限/阈值/最小值。

for(i=0;i<limit;++i){
 //add empty fields to array until limit is reached
}

#5


0  

You could do it with a loop that scans the string searching for the delimiter, then put whatever is not ahead of the delimiter in the array, then re-do it until it hits the top array size, that way it just return whatever was ahead as the last entry in the array:

你可以用一个循环扫描搜索的字符串分隔符,然后把任何不是数组中的分隔符,然后重做一遍,直到达到*数组大小,这样它就返回无论前方是数组中的最后一个条目:

public string[] mySplit(String Split, char Delimiter, int Top)
 {
  string[] returned = new string[Top];
  for(int i = 0; i < Top; i++){
    if(i==Top)
    {
     returned[i] = Split; return;
    }
    else
    {
     int compPlace = 0;
     char comp = Split.toCharArray()[compPlace];
     while(comp != Delimiter)
     {
      compPlace++;
      comp = Split.toCharArray()[compPlace];
      }
    returned[i] = Split.SubString(0,compPlace);
    Split = Split.SubString(compPlace);
    }
  }
  return returned;
}

Im sure youll have to tweak this as i have a little headache as of now :)

我敢肯定你得把这个调整一下,因为我现在有点头疼。

#6


0  

It seems that it would be easy enough to pad out a new array with empty strings. Here is a utility method you could use to perform this split.

用空字符串填充一个新的数组似乎很容易。这里有一个实用方法可以用来执行这个分割。

public static String[] split(Pattern pattern, CharSequence input, int limit) {
    // perform the split
    tokens = pattern.split(input, limit);

    int tokenCount = tokens.length();
    if (tokenCount == limit) {
        // if no padding is necessary, return the result of pattern.split
        return tokens;
    } else {
        // create a new array, copy tokens into array, pad with empty string
        String[] padded = Arrays.copyOf(tokens, limit);
        for (int i = tokenCount; i < limit; i++) {
            padded[i] = "";
        }
        return padded;
    }
}

Edit: Updated to shamelessly steal [Arrays.copyOf][1] from unholysampler's answer.

编辑:更新为无耻地偷取[数组]。copyOf][1]从unholysampler的答案。

[1]: http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(T[], int)

[1]:http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html copyOf(T[],int)

#7


0  

Simply getting the position of the delimiter and using substrings gets you there pretty easily:

简单地获取分隔符的位置并使用子字符串就可以很容易地实现:

public static String[] split(String str, int limit){

           String[] arr = new String[limit];

           for(int i = 0; i < limit-1; i++){
               int position = str.indexOf(',');
               int length = str.length();

               if(position == -1){
                   arr[i]= str.substring(0,length);
                   str = str.substring(length, length);
               }
               else{
                   arr[i] = str.substring(0, position);
                   str = str.substring(position+1, length);
               }
            }

            arr[limit-1] = str;

            return arr;
       }