Understanding of split and stringtokenizer

Source: Internet
Author: User

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 (RegExN)The format is the same as the result of the following expression:

Pattern. Compile(RegEx). Split(StrN)

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 valuenextTokenThe number of times the method is used.

Boolean hasmoreelements ()Returns andhasMoreTokensThe 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 isObjectInsteadStringIn addition, it returnsnextTokenThe 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:

  1. StaticString STR ="I love China";
  2. Public Static VoidSplittest (){
  3. String [] result = Str. Split ("",-1);
  4. For(IntI = result. Length-1; I> =0; I --){
  5. System. Out. Print (result [I] +"");
  6. }
  7. }

Use stringtokenizer:

  1. StaticString STR ="I love China";
  2. Public Static VoidStringtokenizertest (){
  3. Stringtokenizer ST =NewStringtokenizer (STR );
  4. String [] strarray =NewString [st. counttokens ()];
  5. For(IntI = strarray. Length-1; I> =0; I --){
  6. Strarray [I] = ST. nexttoken () +"";
  7. }
  8. For(String S: strarray ){
  9. System. Out. Print (s );
  10. }
  11. }// It seems that this method is a bit redundant, making it easier to use split.

 

Understanding of split and stringtokenizer

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.