Import Java.util.Iterator;
Import Java.util.TreeSet;
/*
* Sort the numbers in a string
* String str = "10,2,11,1,34,101,32";
*/
public class Demo7 {
public static void Main (string[] args) {
String str = "10 2 11 1 34 10132";
TreeSet ts = new TreeSet ();
string[] arr = Str.split ("");
for (int i=0; i<arr.length; i++) {
Ts.add (Integer.parseint (Arr[i]));//Convert to shaping, the elements have a natural order
}
SYSTEM.OUT.PRINTLN (TS);
Using iterators to traverse stitching strings
Iterator iter = Ts.iterator ();
String str1 = "";
while (Iter.hasnext ()) {
str1+= (Iter.next () + "");
}
System.out.println (STR1);
}
}
Key notes:
1. Split the string using split of the String class;
2. Use the parseint of the shaping class to convert the string type into shaping;
3. Create a TreeSet object using the natural ordering function of the TreeSet class;
Using TreeSet to sort pure numeric strings