public class StringDivideTest {
public static void splitTest(String str) {
System.out.println("====== splitTest() result ======");
String split[] = str.split(",");
for (int i = 0; i < split.lenght; i++) {
System.out.println(i + "번째 : " + split[i]);
}
}
public static void stringTokenizerTest(String str) {
System.out.println("====== stringTokenizerTest() result ======");
StringTokenizer tokenizer = new StringTokenizer(str, ",");
for (int i = 0; tokenizer.hasMoreTokens(); i++) {
System.out.println(i + "번째 : " + tokenizer.nextToken());
}
}
public static void main(String[] args) {
String str = "apple,banana,kiwi";
splitTest(str);
System.out.println();
stringTokenizerTest(str);
}
}
String str = "apple,banana,kiwi";
둘 다 동일하게 동작함
<결과>
======= splitTest() result =======
0번째 : apple
1번째 : banana
2번째 : kiwi
======= stringTokenizerTest() result =======
0번째 : apple
1번째 : banana
2번째 : kiwi