Separation of strings in Java "|" "." "*" "_" Special symbol solution __java

Source: Internet
Author: User
Tags int size
StringTokenizer (string-delimited resolution type) Java.util.StringTokenizer

Function: The string is bounded by delimiters, analyzed as a token (can be understood as words), delimiters can be specified themselves.


1. Constructor function.
1. StringTokenizer (String str): Constructs a StringTokenizer object to parse str. The Java default separator is space, tab (' \ t '), line break (' \ n '), ' carriage return (' \ R ').
2. StringTokenizer (String str, string Delim): Constructs a StringTokenizer object to parse STR and provides a specified delimiter. such as New StringTokenizer ("a|001:b|002:c|003", ":");
3. StringTokenizer (String str, String Delim, Boolean returndelims): Constructs a StringTokenizer object to parse STR and provides a specified delimiter, while Specifies whether to return the separator character.
2, method.
Description
1. All methods are public;
2. Writing format: [modifier] < return type > < method name ([parameter list])

1. Int Counttokens (): Returns the number of times the Nexttoken method was invoked. If you use constructors 1 and 2, the number of delimiters is returned (example 2).
2. Boolean hasmoretokens (): Returns whether there are delimiters.
3. Boolean hasmoreelements (): The result is the same as 2.
4. String Nexttoken (): Returns a string from the current position to the next separator.
5. Object nextelement (): The result is the same as 4.
6. String Nexttoken (String Delim): Similar to 4, returns the result with the specified delimiter.
3, examples. () split and StringTokenizer of the rustling difference!

Import Java.util.StringTokenizer;
public class Stringtokenizerdemo {
public static void Main (String args[]) {
String str= "100|66,55:200|567,90:102|43,54";
String abc= "a| b| c| D ";
StringTokenizer strtoke=new StringTokenizer (str, ":");//default does not print separator
StringTokenizer strtoke=new StringTokenizer (str, ":", true);/print Separator
StringTokenizer strtoke=new StringTokenizer (str, ":", false);/Do not print separator
int Size=strtoke.counttokens ();//3 & 5
System.out.println ("Strtoke count =" +size);
while (Strtoke.hasmoreelements ()) {
System.out.println (Strtoke.nexttoken ());
System.out.println (Strtoke.nextelement ()); Effect ditto
}
String[] Str_abc=str.split ("\\|"); /The result is the same as StringTokenizer
String[] Str_abc=str.split ("|"); /Get a different result
for (int i=0;i<str_abc.length;i++) {
System.out.println (Str_abc[i]);
}
}
}


No. 2
ZZ from http://77857.blog.51cto.com/67857/142324

<!> comparison Two representations use the Split function: String s = new string ("2_8_7_4_3_9_1");
string[] arr = S.split ("_");
Using the StringTokenizer class: String s = new string ("2_8_7_4_3_9_1");
StringTokenizer Commatoker = new StringTokenizer (S, "_");
string[] arr = new String[commatoker.counttokens ()];
The <2>split usage in the Java.lang package has the String.Split () method, which returns an array
I use some in the application, for you to sum up, for your reference only:
1. If you use "." As a separate word, it must be written as follows: String.Split ("\"), so that the correct separation can be separated, can not use String.Split (".");
2. If you use "|" As a separate word, must be written as follows: String.Split ("\\|"), so that the correct separation, can not use String.Split ("|");
“.” and "|" All are escape characters, must add "\";
3. If you have more than one separator in a string, you can use the "|" As a hyphen, such as: "Acount=?" and UU =? Or n=? ", the three are separated, 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 results:

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 results we expected if it uses some special characters. We see JDK doc for public string[] split (String regex) splits this string around matches of the given regular. 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 vertical Bars | Delimited string, you will not get the desired result 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 the vertical * separator string will throw the java.util.regex.PatternSyntaxException exception, with the plus sign +. 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, + * is not a valid pattern-matching rule expression, with the "\\*" "\\+" escaped to get the correct result. The "|" divider string, while being able to execute, is not intended, "\\|" The correct result can be obtained after escaping. Also, if you want to use the "\" character in a string, you also need to escape. The first to express "AAAA\BBBB" This string should be used "AAAA\\BBBB", if you want to separate should be so to get the correct result: string[] aa = "AAA\\BBB\\BCCC". Split ("\\\\");


Original link: http://77649119-qq-com.iteye.com/blog/1261553

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.