Develop two methods for string segmentation in JAVA programming
Source: Internet
Author: User
Two Methods for string segmentation in JAVA programming are developed: general Linux technology-Linux programming and kernel information. For details, refer to the following section. Method 1: Use String split. The verification code is as follows:
Import java. 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 = Integer. parseInt (testString);
}
// 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);
}
}
}
Method 2: Use StringTokenizer
Import java. util. Arrays;
Import java. 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= Integer. parseInt (result);
}
// Sort
Arrays. sort ();
// Asc sort
For (int j = 0; j <result. length; j ++ ){
System. out. println (a [j]);
}
}
}
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