1.StringTokenizer的例子
import java.util.StringTokenizer;
public class TestStringTokenizer {
public static void main(String[] args) {
StringTokenizer tokenizer = new StringTokenizer(
"I am a deve,loper; in shenzhen", " |,|;");
System.out.println(tokenizer.countTokens());
while (tokenizer.hasMoreTokens()) {
// logger.debug(tokenizer.nextToken(" ")); //nextToken(String
// delim); 下一个分隔符分割的值
System.out.println(tokenizer.nextToken());
}
}
}
结果:
7
I
am
a
deve
loper
in
shenzhen
2.Split的例子
public static void main(String[] args) {
TestSplit ts = new TestSplit();
System.out.println(ts.bubbleSort("1,32,23 14 5,7"));
}
public static String bubbleSort(String str1) {
String st[] = str1.split(",| ");
for (int i = 0; i < st.length; i++) {
for (int j = 0; j < st.length - 1 - i; j++) {
if (Integer.parseInt(st[j]) > Integer.parseInt(st[j + 1])) {
String temp = st[j];
st[j] = st[j + 1];
st[j + 1] = temp;
}
}
}
String str2 = "";
for (int i = 0; i < st.length; i++) {
if (str2.equals("")) {
str2 = st[i];
} else {
str2 = str2 + "," + st[i];
}
}
return str2;
}
结果:1,5,7,14,23,32