Understanding of split and stringtokenizer
I. Split
Splits the string based on the regular expression matching the given regular expression. The array returned by this method includes the sub-string of this string. Each sub-string is terminated by a sub-string that matches the given expression, or ends at the end of the string. The substrings in the array are arranged in the order they appear in this string. If the expression does not match any part of the input, the resulting array only has one element, that is, this string.
String [] Split (string RegEx, int limit) the use of this method is very specific in the API.
LimitLimit number controls the number of times that the mode applies, thus affecting the length of the obtained array. Suppose this restrictionNIf the value is greater than 0, the mode will be applied to the maximum number of applications.N-Once, the length of the array will not be greaterNAnd the last entry of the array will include all the input that exceeds the last matched delimiter. HypothesisNIf it is not positive, the pattern will be applied as many times as possible, and the array can be whatever length. HypothesisNIf the value is 0, the mode will be applied as many times as possible, the array can be regardless of the length, and the trailing null string will be discarded.
For example, string"Boo: And: foo"The following results can be generated using the number of shards:
RegEx |
Limit |
Result |
: |
2 |
{"Boo", "And: foo "} |
: |
5 |
{"Boo", "and", "foo "} |
: |
-2 |
{"Boo", "and", "foo "} |
O |
5 |
{"B", "", ": And: F ","",""} |
O |
-2 |
{"B", "", ": And: F ","",""} |
O |
0 |
{"B", "", ": And: F "} |
TheStr.Split (RegEx, N)The format is the same as the result of the following expression:
Pattern. Compile(RegEx). Split(Str, N)
Splitting is convenient for string cutting...
Ii. stringtokenizer
The stringtokenizer class agrees that the application splits the string into tokens. This class includes several important methods.
Int counttokens ()Calculate the valuenextToken
The number of times the method is used.
Boolean hasmoreelements ()Returns andhasMoreTokens
The same method value.
Boolean hasmoretokens ()Token to test whether there are many other available tags in the tokenizer string.
Object nextelement ()Except that the declared return value isObject
InsteadString
In addition, it returnsnextToken
The same method value.
String nexttoken ()Returns the next tag of the string tokenizer.
Stringtokenizer is a legacy class reserved for compatibility reasons (although it is not encouraged to use it in new code ). We recommend that you use this function for all users.StringOfSplitMethod or Java. util. RegEx package.
3. One interview question
For a string, words are segmented by spaces. Print words in reverse order (for example, the result of I love China is: China love I)
Usually, every word in the string is saved in string [] and printed in reverse order. here we need to cut the string...
Use split:
- StaticString STR ="I love China";
- Public Static VoidSplittest (){
- String [] result = Str. Split ("",-1);
- For(IntI = result. Length-1; I> =0; I --){
- System. Out. Print (result [I] +"");
- }
- }
Use stringtokenizer:
- StaticString STR ="I love China";
- Public Static VoidStringtokenizertest (){
- Stringtokenizer ST =NewStringtokenizer (STR );
- String [] strarray =NewString [st. counttokens ()];
- For(IntI = strarray. Length-1; I> =0; I --){
- Strarray [I] = ST. nexttoken () +"";
- }
- For(String S: strarray ){
- System. Out. Print (s );
- }
- }// It seems that this method is a bit redundant, making it easier to use split.
Understanding of split and stringtokenizer