java USES split to split strings last Spaces etc

  • 2020-06-15 08:25:47
  • OfStack

When using split in java to split the string according to "\t", it was found that if the last few fields were empty and were only divided by \t, which were attached \t, it would not be split, such as "d\tc\ t\t\t", the last few \t would not be split and should be processed before splitting the string.

Error in the number of split fields caused. Check 1 APi, need to add parameter -1 in split,String[] values = line. split("\t",-1);

The imit parameter controls the number of times the pattern is applied, thus affecting the length of the resulting array

If the limit n is greater than zero, the pattern applies n at most > - once, the length of the array is no longer than n, and the last entry of the array will contain all input except the last matching delimiter

If n is not positive, the number of times the pattern is applied is unlimited, and the array can be of any length

If n is zero, the number of times the pattern is applied is unlimited, the array can be of any length, and the trailing empty string is discarded (for this reason)

Once you add the parameter -1, you're done.


Related articles: