String.split () encountered several situations empty string

Suddenly encountered such a problem at work today, all on one line in the data file when using split cut time and space are no statistics on, people very angry, looking for a bit split function of other uses to know the reason, Yi Yi what. . .

The Java split we can use the string dividing the specified delimiter, and returns an array of strings
   split method
      role of this method are: dividing a substring of a string, and then return the result as an array of strings.
      stringObj.split ([Separator, [limit]]) 
       stringObj
             Required. String object or text to be broken down, the object will not be modified split method.
       separator 
            options. String or a regular expression object, which identifies the partition string is used for one or multiple characters. If omitted, returns a single-element array containing the entire string. 
       limit
            the number of parameter control mode application, and therefore affects the length of the resulting array. If the limit is greater than n 0, the mode is most widely n - 1 times, the length of the array will not be greater than n, and a last array will contain all of the input beyond the last delimiter matching. If n is non-positive, then the pattern to be applied as many times, and the array may be any length. If n is 0, then the pattern to be applied as many times, the array may be any length, and a null-terminated string is discarded.

You encounter the following situations:

1. empty strings are not resolved


public class test {
	public static void main(String[] args) {
		String str = "1,2,3,4,,,";
		String[] arr = str.split(",");
		for (String string : arr) {
			System.out.println("str"+string);
		}
		System.out.println(arr.length);
	}
}

 

Result 1:

2. Empty the last delimiter character string is not divided, the remaining empty string can be resolved.


public class test {
	public static void main(String[] args) {
		String str = "1,2,3,4,,,5";
		String[] arr = str.split(",");
		for (String string : arr) {
			System.out.println("str"+string);
		}
		System.out.println(arr.length);
	}
}

Results:

But when actual development does not guarantee a final separator string is divided is not empty, so with the split () method then takes this array of strings separated by time and sometimes data on cross-border problems (last a separator string is empty points)
     think of a method is split ([separator, [limit] ]), assigned to the back of the limit values is divided into a large number of characters after the length of the array than the string array, this ensures that all empty can be resolved, which is to be the api in explanation:

3. If the n is greater than 0 limitation, the most widely used model is n - 1 times (where n is 1000)
 

public class test {
	public static void main(String[] args) {
		String str = "1,2,3,4,,,";
		String[] s = str.split(",",1000);
		for (String string : s) {
			System.out.println("str"+string);
		}
		System.out.println(s.length);
	}

 Results:

4. If the limit setting non-positive, as can be all resolved:

public class test {
	public static void main(String[] args) {
		String str = "1,2,3,4,,,";
		String[] s = str.split(",",-1);
		for (String string : s) {
			System.out.println("str"+string);
		}
		System.out.println(s.length);
	}
}

 Result 4:

When String str = "1,2,3,4 ,,, 5"; coincides with the above results were 3,4.

1. When the parameter is an integer of time, just before the interception of a few, a few require several interception, this goes without saying. 


Java code 
String = Line "AA, BB, CC, dd ,,,,";  
System.out.println (line.split ( ",",. 6) .length);  
The output is 6, limit specify several parameters, several output, up to 8  
 

2. When the parameter is zero time, and split () as a string as many shots (in fact, is not the most). 
Java code  
String = Line "AA, BB, CC, dd ,,,,";  
System.out.println (line.split ( ",", 0) .length);  
output 4   


3. When the parameter is negative, even though the back empty string, the maximum output will be 
Java code  
String = Line "AA, BB, CC, dd ,,,,";  
System.out.println (line.split ( ",", - 1) .length   );
output 8   


: Attention
. "" 1, if used as a separator, it must be written as follows, String.split ( "\\.") , So as to properly separated, can not be used String.split; (. "")
2, If "|" as a separator, it must be written as follows, String.split ( "\\ |") , in order to properly separated, can not be used String.split ( "|");

reprinted original: https: / /blog.csdn.net/qqxyy99/article/details/78861109 
 

 

Guess you like

Origin blog.csdn.net/weixin_39921821/article/details/89489031
Recommended