컴퓨터공부/Kotlin & Java

StringTokenizer 예제

achivenKakao 2017. 12. 22. 01:51

StringTokenizer 클래스는 String.split()와 비슷하지만 또 다르다.

사실 두개가 무슨 큰 차이인가 하는 생각이 들기도 한다. 어떤걸 쓰더라도 문제 없이 쓰면 되는거 아닌가?

그래도 두개의 차이는 존재 한다. 


String.split()에 비해 StringTokenizer은 여러 문자에 대해서도 한번에 파싱이 가능하다.

아래와 같이 "!"와 "/"를 한 번에 파싱할 수 있다.

String str = "1/2!3/4!5";
StringTokenizer tokenizer = new StringTokenizer(str, "/!");

결과 > 


12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 1

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 2

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 3

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 4

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 5



자세한건 참고를 보면 좋을 것 같고,

레퍼런스처럼 사용되는 코드를 첨부 하면서 포스팅을 마무리 한다.  


String str = "1/2!3/4!5";
StringTokenizer tokenizer = new StringTokenizer(str, "/!");

while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken() + "\n");
}
System.out.println("=============================\n");

String strSplit = "1/2/3/4/5";
String str2[] = strSplit.split("/");
for(int i =0; i < str2.length; i++){

    System.out.println(str2[i] + "\n");


결과 >


12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 1

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 2

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 3

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 4

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 5

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: =============================

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 1

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 2

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 3

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 4

12-22 01:45:42.486 13023-13023/com.study.practice.stringtokenizer I/System.out: 5






참고 : http://hunit.tistory.com/166

http://blog.naver.com/PostView.nhn?blogId=ilovejsp&logNo=100126488795