Java.split (); usage

Source: Internet
Author: User

Split usage Description There is a String.Split () method in the Java.lang package, which returns an array
In the application of some, for everyone to summarize, for your reference only:
1. 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 (".");
2. If you use "|" As a separate word, it must be written as follows: String.Split ("\\|"), so as to properly separate, can not be used String.Split ("|");
“.” and "|" is an escape character and must be added "\ \";
3. If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example: "Acount=?" and UU =? Or n=? ", the three are separated out, you can use String.Split (" And|or ");
The test procedure is as follows: public class Stringsplit {/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
String a= "acount=? and UU =? or n=? ";
String b[]=a.split ("And|or");
for (int i=0;i<=b.length;i++)
{
System.out.println (B[i]);
}   }}
Output result: acount=?
UU =?
N=?
Exception in thread "main" Java.lang.arrayindexoutofboundsexception:3
At Com.ljj.string.StringSplit.main (stringsplit.java:14)

No.3


String.Split method

Issues to be aware of when using the String.Split method when separating strings using the String.Split method, the delimiter 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 expression. The parameter regex is A regular-expression match pattern instead of a simple string, he may have unexpected results for some special characters, such as testing the following code: using a vertical bar | Separating the strings, you will not get the expected results string[] AA = "AAA|BBB|CCC". Split ("|");
string[] aa = "AAA|BBB|CCC". Split ("\\|"); This will get the correct result for (int i = 0; I System.out.println ("--" +AA);
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 ("\\*"); This will get the correct result for (int i = 0; I System.out.println ("--" +AA);
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. First to express "AAAA\BBBB" This string should be used "AAAA\\BBBB", if you want to separate it should be so to get the correct result: string[] aa = "AAA\\BBB\\BCCC". Split ("\\\\");

Java.split (); usage

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.