티스토리 뷰

Programming Language/JAVA

StringTokenizer

기내식은수박바 2019. 9. 6. 22:34
반응형

String.split(String regex) / StringTokenizer

  • split 메소드는 String 클래스의 메소드로서 regex를 기준으로 나눈 문자열 배열을 반환한다.
  • StringTokenizer는 메소드가 아닌 클래스이며, 문자열을 구분자로 분리한 후 문자열 토큰을 만든다.
  • StringTokenizer는 Enumeration 인터페이스를 구현하였기 때문에 next~() 메소드를 사용하여 토큰을 하나씩 꺼낼 수 있다.

 

hasMoreElements() / hasMoreTokens()

  • 두 메소드의 성능적인 차이점은 없으며, Java api doc의 설명은 다음과 같다.

hasMoreElements()

  • Returns the same value as the hasMoreTokens method. It exists so that this class can implement the Enumeration interface.
  • hasMoreTokens 메소드와 동일한 값을 반환한다. 이 클래스가 Enumeration 인터페이스를 구현할 수 있기 때문에 존재한다.

hasMoreTokens()

  • Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.
  • 이 Tokenizer의 문자열에서 더 많은 토큰을 사용할 수 있는지 여부를 검사한다. 이 메소드가 true를 반환하면, 파라미터 없는 다음 nextToken은 성공적으로 토큰을 반환할 것이다.

Conclusion

  • 보통 hasMoreTokens() 를 사용하며, 그 이유는 메소드 이름을 통해 직관적으로 어떠한 기능을 하는지 알 수 있기 때문이다.
  • nextElement() 와 nextToknes() 도 위와 같은 경우이므로, 보통 nextTokens() 를 사용한다.

 

Return Type Method Description
Constructor (생성자) StringTokenizer(String str) 문자열 str을 가진 StringTokenizer 객체를 생성한다.

StringTokenizer

(String str, String delim)

문자열 str을 가진 StringTokenizer 객체를 생성한다. 그리고

delim문자열을 이용하여 str문자열을 분리한 토큰들을 가지고 있는다.

(ex. st = new StringTokenizer("12121d1121212", "d")

result (st tokens) : 12121, 1121212)

StringTokenizer

(String str, String delim,

boolean returnDelims)

위 생성자와 다른 점은 returnDelims = true라면 delim 문자열도 토큰으로 포함된다. (디폴트 값은 false)

(ex. st = new StringTokenizer("12121d1121212", "d", true)

result (st tokens) : 12121, d, 1121212)

int countTokens() 현재 객체에 남아있는 토큰의 갯수를 반환한다. next~() 메소드를 통해 토큰이 빠지면 countTokens()의 반환 값은 1씩 줄어든다.
boolean hasMoreElements() 객체에 토큰이 남아있다면 True, 없다면 False를 반환한다.
boolean hasMoreTokens() 객체에 토큰이 남아있다면 True, 없다면 False를 반환한다.
Object nextElement() 객체에 있는 토큰을 하나씩 읽어오며, 객체 내 토큰이 하나씩 빠지게 된다.
String nextToken() 객체에 있는 토큰을 하나씩 읽어오며, 객체 내 토큰이 하나씩 빠지게 된다.
String nextToken(String delim) 객체에 있는 토큰을 하나씩 읽어온다.

 

 

References

 

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함