Trivial-about StringTokenizer, stringtokenizer
Belongs to: java. util package
Constructor:
1. StringTokenizer (String str): constructs a StringTokenizer object used to parse str. The default delimiter for java is space, tab ('\ t'), line feed (' \ n'), and carriage return ('\ R ')".
2. StringTokenizer (String str, String delim): constructs a StringTokenizer object used to parse str and provides a specified separator.
3. StringTokenizer (String str, String delim, boolean returnDelims): constructs a StringTokenizer object for parsing str, provides a specified separator, and specifies whether to return a separator.
In Java, StringTokenizer is a legacy class reserved for compatibility reasons (although it is not encouraged in new code ). We recommend that you use this function for all users.StringOfSplitMethod or java. util. regex package.
However, many of the latest hadoop code still uses it.
Two differences
For a string, words are separated by spaces and printed in reverse order (for example, the result of I Love China is: China Love I)
The general idea is to save each word in the String in String [] and print it in reverse order. here we need to split the String...
Use split:
1 static String str = "I Love China";2 public static void splitTest() {3 String[] result = str.split(" ",-1);4 5 for(int i = result.length - 1; i >= 0; i--) {6 System.out.print(result[i] + " ");7 }8 }
Use StringTokenizer:
1 static String str = "I Love China"; 2 public static void StringTokenizerTest () {3 StringTokenizer st = new StringTokenizer (str); 4 String [] strArray = new String [st. countTokens ()]; 5 6 for (int I = strArray. length-1; I> = 0; I --) {7 strArray [I] = st. nextToken () + ""; 8} 9 10 for (String s: strArray) {11 System. out. print (s); 12} 13} // This method seems to be a bit redundant, making it easier to use split.
StringTokenizer usage
StringTokenizer is a legacy class reserved for compatibility reasons (although it is not encouraged in new code ). We recommend that you use the split method of String or the java. util. regex package.
It is easy to use regular expressions.
String input = "aZ8sd★Yu ";
String [] result = input. split "^ [a-zA-Z]";
I haven't used a regular expression for a long time, and I don't know whether it is correct or not...
Hope to help you
The string tokenizer class allows applications to break strings into tags. The tokenization method is simpler than the method used by the StreamTokenizer class. The StringTokenizer method does not distinguish between identifiers, numbers, and strings with quotation marks. They also do not recognize and skip comments.
It can be specified at creation, or a set of separators (separated characters) can be specified based on each tag.
The StringTokenizer instance has two behavior modes, depending on whether the value of the returnDelims flag used during creation is true or false:
If the flag is false, delimiter characters are used to separate tags. Mark is the maximum sequence of consecutive characters (not delimiters.
If the flag is true, the delimiter characters are considered as tags. Therefore, the mark is either a delimiter or the maximum sequence of consecutive characters (not separators.
The current position of the StringTokenizer object to be marked in the internal maintenance string. Some operations Move the current position after the processed characters.
Returns a token by intercepting a substring of a string, which is used to create a StringTokenizer object.
The following is an instance using tokenizer. The Code is as follows:
StringTokenizer st = new StringTokenizer ("this is a test ");
While (st. hasMoreTokens ()){
System. out. println (st. nextToken ());
}
Output The following string:
This
Is
A
Test
StringTokenizer is a legacy class reserved for compatibility reasons (although it is not encouraged in new code ). We recommend that you use the split method of String or the java. util. regex package.
The following example illustrates how to use the String. split method to break a String into basic tags:
String [] result = "this is a test". split ("\ s ");
For (int x = 0; x <result. length; x ++)
System. out. println (result [x]);
Output The following string:
This
Is
A
Test... remaining full text>
StringTokenizer
Public class StringTokenizerTest {
Public static void main (String [] args ){
String a = "a B c ";
StringTokenizer s = new StringTokenizer (a, "\ t", false );
System. out. println (s. toString ());
While (s. hasMoreTokens ()){
String temp = s. nextToken ();
System. out. print ("|" + temp + "| ");
System. out. println (temp. length ());
}
System. out. println ("-------------------------");
String [] ss = a. split ("\ t ");
For (String temp: ss ){
System. out. print ("|" + temp + "| ");
System. out. println (temp. length (); // The length of the string object obtained from the first and second tabs is 0.
}
}
}
====================================
Java. util. StringTokenizer @ 192d342
| A | 1
| B | 1
| C | 1
-------------------------
| A | 1
| 0
| B | 1
| C | 1
The definition of string a is as follows:
'A' + tab + 'B' + tab + 'C'