Method 1: Use string split. The verification code is as follows:
ImportJava. Util. arrays;
Public class testsplit {
Public static void main (string [] ARGs ){
String orignstring = new string ("5, 8, 7, 4, 3, 9, 1 ");
String [] teststring = orignstring. Split (",");
Int [] test = {0, 0, 0, 0, 0, 0, 0 };
// String to int
For (INT I = 0; I <teststring. length; I ++ ){
Test [I] = integer. parseint (teststring [I]);
}
// Sort
Arrays. Sort (test );
// ASC sort
For (Int J = 0; j <test. length; j ++ ){
System. Out. println (test [J]);
}
System. Out. println ("Next ");
// DESC
For (INT I = (test. Length-1); I> = 0; I --){
System. Out. println (test [I]);
}
}
}
Method 2: Use stringtokenizer
ImportJava. Util. arrays;
ImportJava. Util. stringtokenizer;
Public class splitstringtest {
Public static void main (string [] ARGs ){
String S = new string ("5, 8, 7, 4, 3, 9, 1 ");
Int length = S. Length ();
// Split S ","
Stringtokenizer commatoker = new stringtokenizer (S ,",");
String [] result = new string [commatoker. counttokens ()];
Int K = 0;
While (commatoker. hasmoretokens ()){
Result [k] = commatoker. nexttoken ();
K ++;
}
Int [] A = new int [result. Length];
For (INT I = 0; I <result. length; I ++ ){
A [I] = integer. parseint (result [I]);
}
// Sort
Arrays. Sort ();
// ASC sort
For (Int J = 0; j <result. length; j ++ ){
System. Out. println (A [J]);
}
}
}