Java Split split string usage

Source: Internet
Author: User

There is a String.Split () method in the Java.lang package, and the return is an array.
1,"." and "|" is an escape character and must be added "\ \";
If you use "." As a separate word, it must be written as follows:
String.Split ("\ \"), in order to properly separate, can not be used String.Split (".");
If you use the ' | ' As a separate word, it must be written as follows:
String.Split ("\\|"), so as to properly separate, can not use String.Split ("|");

2. If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example: "Acount=?" and UU =? Or n=? ", separating the three, can be used
String.Split ("And|or");

3, public string[] Split (string regex,int limit) splits this string by matching a given regular expression.
The array returned by this method contains each substring of this string, terminated by another substring that matches the given expression, or terminated by the end of the string. In the array
Of substrings are arranged in the order in which they are in this string. If the expression does not match any part of the input, the resulting array has only one element, that is, the string.

4. Public string[] Split (string regex)
The name of the parameter here is the Regex, which is the regular expression (regex). This parameter is not a simple segmented character, but a regular expression,
He may have unexpected results for some special characters, such as testing the following code:

(1) with vertical bar | Delimited string, you will not get the expected result

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.; I++ { system.. Println+aa }

(2) 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.; I++ { system.. Println+aa }

          (3) Obviously, + * is not a valid pattern-matching rule expression, with "\\*" "\\+" The correct results can be obtained after escaping.  
          (4) "|" While the string is executed, but not intended, "\\|" The correct results can be obtained after escaping.  
          (5) Also, if you want to use the "" character in a string, you also need to escape. The first thing to say about "aaaa\bbbb" is to use "aaaa\\bbbb", If you want to separate, you should be able to get the correct result:  
string[] aa = "AAA\\BBB\\BCCC". Split (\\\\);  
          (6) There is also the dot ".", also to be escaped first to get the correct result.


First method:  

string S= "ABCDEABCDEABCDE" string[] Sarray=s. Split ( ' C ' ) ;foreach (string I in< Span class= "PLN" > 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:

StringS="ABCDEABCDEABCDE"; String[]SArray1=s. Split (new char[3]{ ' C ' , ' d ' , ' E ' }) ; Span class= "KWD" >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;

Using System.Text.RegularExpressions StringContent=AgcsmallmacsmallgggsmallYtx; string[] Resultstring= regex. Split (content,smallregexoptions. Ignorecase foreach ( I in Resultstring) console. Writeline (i. Tostring

Output the following result:
Agc
Mac
Ggg
Ytx

The fourth method:

StringStr1=* * * * * * * * * * * * * * * * * * * * * * *; String[]str2;Str1=Str1replace (, "*" Span class= "pun" >) ;str2=str1. Split ;foreach (string I in< Span class= "PLN" > Str2) console.writeline (i. Tostring


The Fifth method:
String Str1= "I * * * * * * * * * * * * * * * * * * * * * *;
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:

StringStr1= ' I * * * * * * * * * * * * * * * * * * * * teacher string[] str2 = system. Text. Regularexpressions. Regex. Splitstr1,@[*]+); foreach (string I in< Span class= "PLN" > Str2) console.writeline (i. Tostring

Here through [*]+ cleverly completed our goal.

Java Split split string usage

Related Article

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.