Split method of String class in J2SE basic compaction Series

Source: Internet
Author: User

This split method of the Strig class has been used many times. I have referred to a lot of online materials and summarized it now.
1: usage of the second parameter of the split Function
I saw a problem on the Forum, and then I became interested in further exploring this function:
First, use the split with only one parameter:
Public class SplitTest {
/**
* @ Param args
*/
Public static void main (String [] args ){
String str = "what, is, that ";
String [] strs = str. split (",");
System. out. println (strs. length );
}
 
}

The output value is 6. However, if the string is changed
[Java] view plaincopy

"What, is? It looks like 5. Otherwise, for a function split with only one parameter, the output result is 2.
Test code:
<Pre name = "code" class = "java"> public class SplitTest {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
String str = "what, is ,,,,";
String [] strs = str. split (",");
System. out. println (strs. length );
}
 
}
The question is, how can we
"What, is ,,,,";

This string is separated by commas, and if there is no content between commas, it is stored as null?
The second split function can be used to set the second parameter to-1. The test code is as follows:
Public class SplitTest {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
String str = "what, is ,,,,";
String [] strs = str. split (",",-1 );
System. out. println (strs. length );
}
 
}
The result is 6;
So what is the int type parameter behind? It is very easy to set the threshold value of the string to be captured using the split function. -1 indicates no limit. Although the split function with only one parameter is not limited, in that case, if there is no separator between them, it will not be intercepted.
Tips for the second split method

In the java. lang Package, the String. split () method returns a struct array, but there are some tips during use.
For example, Run "2 | 33 | 4". split ("| ")
The result is:
""
2
|
3
3
|
4
It's strange, but check the API description to find out why.
 
Java. lang. string. split
Split Method
Splits a string into substrings and returns the result as a string array.
StringObj. split ([separator, [limit])
Parameters
StringObj
Required. String object or text to be decomposed. This object will not be modified by the split method.
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.
Limit
Optional. This value is used to limit the number of elements in the returned array.
Description
The result of the split method is a string array. In stingObj, the position where separator appears must be decomposed.
 
 
Therefore, the normal syntax is as follows:
1. If ". ", must be written as follows: String. split ("\\. "), in this way, the correct separation is not allowed using String. split (". ");
2. If "|" is used as the separator, it must be written as follows: String. split ("\ |"), in order to correctly separate, cannot use String. split ("| ");
"." And "|" are both escape characters and must be added with "\"; (here is a point of Regular Expression in java)
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, "a = 1 and B = 2 or c = 3 ", you can use String to separate all three. split ("and | or ");

Problems caused by tri-string Inversion
The questions are as follows:
String str = "I am a programmer"; the number of spaces is 1, 2, and 3 respectively.
It is required to follow the word inversion, that is, the final output "programmer a am I"; the number of spaces is 3, 2, 1, respectively.
 
1: Use the split function method:
Public static void reverse (){
String s = "I am a programmer"; // The numbers of spaces are 1, 2, and 3, respectively.
String [] eg = s. split ("",-1 );
String result = "";
For (int I = eg. length-1; I> 0; I --){
Result + = eg [I] + "";
}
Result + = eg [0]; // note that spaces cannot be added before the first character. Otherwise, a space is added, so traverse from the back to the front
System. out. println (result );
There is always a fuzzy point here, that is, if multiple consecutive spaces are combined, but only one space is required for separation, and other spaces cannot be deleted, what should we do? You need to see how the split function works. For visualization, replace spaces with ",".

String oldStr = "z, z ";
String [] strs = oldStr. split (",");
For (String str: strs ){
System. out. println (str );
}
You can change the number of "and" between strings to view. From the running result, you can see that when you split the string "z, z, first, the string is divided into "z" and ", z" two strings, the second step operation string ", z", divided into an empty string and ", z" string, it is further divided into an empty string and a z. The final result is as follows: the two rows in the middle are null.
Z
 
 
Z
 
2: Do not use the split function:
String oldstr = "I am a programmer ";
Int index = oldstr. lastIndexOf ("");
System. out. println (index );
While (index> = 0 ){
System. out. print (oldstr. substring (index + 1) + "");
Oldstr = oldstr. substring (0, index );
Index = oldstr. lastIndexOf ('');
}

System. out. println (oldstr );
In order to clearly see the running result, we replace the space with ",".
Viewplainprint?
String oldstr = "I, am, a, programmer ";
Int index = oldstr. lastIndexOf (",");
While (index> = 0 ){
System. out. print (oldstr. substring (index + 1) + ",");
Oldstr = oldstr. substring (0, index );
Index = oldstr. lastIndexOf (',');
System. out. println (index );
}
In this case, if there are two points in it, if it is unclear, it will lead to uncomprehension. First, the subString (index) method is a string whose value starts from the index character. The string of the second subString (0, index) does not contain the index character.


Author: Allen_Zhao_2012

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.