Java string.split () Usage Summary

Source: Internet
Author: User

It's Java.lang . in the package The String.Split () method, which returns an array

1, if string.split ("\ \") in order to be properly separated and not used string.split (".");
2, if using "|" As a separate word, it must be written as follows: string.split ("\\|") in order to properly separate and not use string.split ("|");
"." and "|" is an escape character and must be added 3, if there is more than one delimiter in a string, you can use "|" As hyphens, such as: "acount=?" and UU =? Or n=? ", the three are separated out, you can use string.split (" And|or ");
When separating strings using string.split methods, separators may not get the result we expect if some special characters are used.

We look at the JDK Doc description

Public string[] split(String regex)

Splits this string around matches of the given regular expression.

Parameter regex is a regular-expression matching pattern instead of a simple String, and he may have unexpected results for some special characters, such as testing the following code:

you will not get the expected results by separating the strings with a vertical bar |

string[] aa = "AAA|BBB|CCC". Split ("|");
string[] aa = "AAA|BBB|CCC". Split ("\\|"); So we can get the right results.

for (int i = 0; I <aa.length; i++) {
System.out.println ("--" +aa[i]);
}

Running with a vertical * delimited string will throw a java.util.regex.PatternSyntaxException exception, with a plus +.

string[] aa = "AAA*BBB*CCC". Split ("*");
string[] aa = "AAA|BBB|CCC". Split ("\\*"); So we can get the right results.

for (int i = 0; I <aa.length; i++) {
System.out.println ("--" +aa[i]);
}

Obviously, the+ * is not a valid pattern-matching rule expression, and the correct result can be obtained after escaping with "\\*" "\\+".

The "|" string is executed, but not intended,"\\|" The correct results can be obtained after escaping.

Also, if you want to use the "" character in a string , you also need to escape . The first thing to say is " aaaa\bbbb" The string should be "AAAA\\BBBB", if you want to separate it to get the correct result:

string[] aa = "AAA\\BBB\\BCCC". Split ("\\\");

The first method:
String S=abcdeabcdeabcde;
String[] Sarray=s.split (' C ');
foreach (String i in Sarray)
Console.WriteLine (i.ToString ());
Output the following result :
Ab
Deab
Deab
De

The second method:
We see the result is a segmentation with a specified character. Use a different construction method to split multiple characters :
String s= "ABCDEABCDEABCDE";
String[] Sarray1=s.split (new char[3]{' C ', ' d ', ' e '});
foreach (String i in SArray1)
Console.WriteLine (i.ToString ());
You can output the following results:
Ab
Ab
Ab

The third method:
In addition to these two methods, the third method is to use regular expressions. Create a new console project. Then the using System.Text.RegularExpressions is added first ;
System.Text.RegularExpressions


String Content=agcsmallmacsmallgggsmallytx;
String[]resultstring=regex.split (Content,small,regexoptions.ignorecase)
foreach (String i in resultstring)
Console.WriteLine (i.ToString ());
Output the following result :
Agc
Mac
Ggg
Ytx

The fourth method:
String str1= I * * * * * * * * * * * * * * * * * * * teacher ;
String[] str2;
Str1=str1. Replace (*****,*);
Str2=str1. Split (*);
foreach (String i in str2)
Console.WriteLine (i.ToString ());

The Fifth method:
String str1= Me* * isOneATeaching * * * * Division ;
I want to show the result : I am a teacher.
If I use the fourth method above, it will produce the following error: I am a teacher. There is a space in the middle of the output, so the output is not the result of hope, this is back to the regular expression, then you can use the following fifth method:
String str1= I * * * * * * * * * * * * * * * * * * * teacher ;
string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@[*]+);
foreach (String i in str2)
Console.WriteLine (i.ToString ());
Here through [*]+ cleverly completed our goal.

Java string.split () Usage Summary

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.