Java String Parsing

Source: Internet
Author: User

Stringtokenizer tokenizer = new stringtokenizer (number ,",");
Boolean bool = true;
While (tokenizer. hasmoretokens ()){
Try {
Double. valueof (tokenizer. nexttoken ());
} Catch (exception e ){
Bool = false;
}
}
// Method for converting a string into an array
Int GV [];
Int I = 0;
Stringtokenizer tokenizer = new stringtokenizer (goodsvolume ,",,");
GV = new int [tokenizer. counttokens ()]; // dynamically determines the length of the array.
While (tokenizer. hasmoretokens ()){
String d = tokenizer. nexttoken ();
GV [I] = integer. valueof (d); // converts a string to an integer.
I ++;
}

// String Parsing
Private string [] stringanalytical (string STR, string divisionchar ){
String string [];
Int I = 0;
Stringtokenizer tokenizer = new stringtokenizer (STR, divisionchar );
String = new string [tokenizer. counttokens ()]; // dynamically determines the length of the array.
While (tokenizer. hasmoretokens ()){
String [I] = new string ();
String [I] = tokenizer. nexttoken ();
I ++;
}
Return string; // returns a string array.
}

Int counttokens ()
Count the number of times the tokenizer nexttoken method can be called before an exception is generated.
Boolean hasmoreelements ()
Returns the same value as the hasmoretokens method.
Boolean hasmoretokens ()
Test whether there are more available tags in the tokenizer string.
Object nextelement ()
Except that the declared return value is an object rather than a string, it returns the same value as the nexttoken method.
String nexttoken ()
Returns the next tag of the string tokenizer.
String nexttoken (string delim)
Returns the next tag in the string of this string tokenizer.

Public class stringanalytical {

// String Parsing. Convert the string to a String Array Based on the delimiter
Private string [] stringanalytical (string, char c ){
Int I = 0;
Int COUNT = 0;

If (string. indexof (c) =-1)
Return New String [] {string}; // If the Delimiter is not included, the return character itself
Char [] cs = string. tochararray ();
Int length = cs. length;
For (I = 1; I <length-1; I ++) {// filter out the first and last separators.
If (CS [I] = c ){
Count ++; // obtain the number of delimiters
}
}
String [] strarray = new string [count + 1];
Int K = 0, j = 0;
String STR = string;
If (k = Str. indexof (c) = 0) // remove the first character as a separator
STR = Str. substring (k + 1 );
If (Str. indexof (c) =-1) // checks whether a separator exists. If not, a string is returned.
Return New String [] {STR };
While (k = Str. indexof (c ))! =-1) {// when a string contains a delimiter
Strarray [J ++] = Str. substring (0, k );
STR = Str. substring (k + 1 );
If (k = Str. indexof (c) =-1 & Str. Length ()> 0)
Strarray [J ++] = Str. substring (0 );
}
Return strarray;
}

Public void printstring (string [] S ){
System. out. println ("*********************************");
For (string STR: S)
System. Out. println (STR );
}

Public static void main (string [] ARGs ){
String [] STR = NULL;
Stringanalytical string = new stringanalytical ();
STR = string. stringanalytical ("1aaa ",'@');
String. printstring (STR );
STR = string. stringanalytical ("2AAA @",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 3aaa ",'@');
String. printstring (STR );
STR = string. stringanalytical ("4aaa @ BBB ",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 5aaa @ BBB ",'@');
String. printstring (STR );
STR = string. stringanalytical ("6aaa @ BBB @",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 7aaa @",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 8aaa @ BBB @",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 9aaa @ BBB @ CCC ",'@');
String. printstring (STR );
STR = string. stringanalytical ("@ 10aaa @ BBB @ CCC @ eee ",'@');
String. printstring (STR );
}
}

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.