๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์–ธ์–ด/C#. JAVA

[JAVA] StringTokenizer ์‚ฌ์šฉ๋ฒ•

by lee365 2021. 1. 5.
๋ฐ˜์‘ํ˜•

๐Ÿ“š ์ƒ์„ฑ์ž(Constructor)

public StringTokenizer(String str);
public StringTokenizer(String str, String delim);
public StringTokenizer(String str, String delim, boolean returnDelims);

 

Parameters

  str                       : ๋ถ„๋ฆฌํ•  ๋ฌธ์ž์—ด

  delim                 : ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•  ๋ฌธ์ž

  returnDelims : ๋ถ„๋ฆฌ๋ฌธ์ž๊นŒ์ง€ ํ† ํฐ์œผ๋กœ ํฌํ•จํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •

                                 true์ด๋ฉด delim ํฌํ•จ, false์ด๋ฉด delim๋ฏธํฌํ•จํ•œ๋‹ค. 

 

๐Ÿ”” ์ฃผ์š”ํ•จ์ˆ˜

int countTokens() 

 ๋‚จ์•„์žˆ๋Š” token์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (ํ˜„์žฌ ๋‚จ์•„์žˆ๋Š” token์˜ ๊ฐœ์ˆ˜)

 

String nextToken()

 ๋‹ค์Œ token์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

boolean hasMoreTokens()

 ๋ฆฌํ„ดํ•  ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด true๋ฅผ ๋‹ค์Œ ํ† ํฐ์ด ์—†์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

 

๐Ÿ“– ์˜ˆ์ œ

1. Default Delim์„ ์ด์šฉ

import java.util.StringTokenizer;
class Solution {
    public static void main(String[] args)
    {
        String str = "ABC DEF GHI";

        // delim์„ ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด default๋กœ ๊ณต๋ฐฑ
        StringTokenizer tokenizer = new StringTokenizer(str);

        System.out.println("์ถ”์ถœ ์ „ token์˜ ์ˆ˜: " + tokenizer.countTokens());

        // ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด while๋ฃจํ”„๋ฅผ ํƒ„๋‹ค.
        while(tokenizer.hasMoreTokens()){ System.out.println(tokenizer.nextToken()); }
        
        System.out.println("์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: " + tokenizer.countTokens());
    }
}
๐Ÿ” ์‹คํ–‰๊ฒฐ๊ณผ 

์ถ”์ถœ ์ „ token์˜ ์ˆ˜: 3  
ABC  
DEF  
GHI  
์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: 0

 

 

2. ํŠน์ • Delim์„ ์ด์šฉ

import java.util.StringTokenizer;
class Solution {
    public static void main(String[] args)
    {
        String str = "ABC-DEF-GHI";

        // "-" ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•œ๋‹ค.
        StringTokenizer tokenizer = new StringTokenizer(str, "-");

        System.out.println("์ถ”์ถœ ์ „ token์˜ ์ˆ˜: " + tokenizer.countTokens());

        // ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด while๋ฃจํ”„๋ฅผ ํƒ„๋‹ค.
        while(tokenizer.hasMoreTokens()){ System.out.println(tokenizer.nextToken()); }
        
        System.out.println("์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: " + tokenizer.countTokens());
    }
}

 

๐Ÿ” ์‹คํ–‰๊ฒฐ๊ณผ 

์ถ”์ถœ ์ „ token์˜ ์ˆ˜: 3  
ABC  
DEF  
GHI  
์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: 0

 

 

3. Delim๊นŒ์ง€ ํฌํ•จ

import java.util.StringTokenizer;
class Solution {
    public static void main(String[] args)
    {
        String str = "ABC-DEF-GHI";

        // "-" ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜๊ณ , ๋ถ„๋ฆฌ๋ฌธ์ž๋„ ํ† ํฐ์— ํฌํ•จ์‹œํ‚จ๋‹ค.
        StringTokenizer tokenizer = new StringTokenizer(str, "-", true);

        System.out.println("์ถ”์ถœ ์ „ token์˜ ์ˆ˜: " + tokenizer.countTokens());

        // ๋‹ค์Œ ํ† ํฐ์ด ์žˆ์œผ๋ฉด while๋ฃจํ”„๋ฅผ ํƒ„๋‹ค.
        while(tokenizer.hasMoreTokens()){ System.out.println(tokenizer.nextToken()); }
        
        System.out.println("์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: " + tokenizer.countTokens());
    }
}

 

๐Ÿ” ์‹คํ–‰๊ฒฐ๊ณผ 

์ถ”์ถœ ์ „ token์˜ ์ˆ˜: 5
ABC
-  

DEF
-  

GHI  
์ถ”์ถœ ํ›„ token์˜ ์ˆ˜: 0

 

 

 

*์ฐธ๊ณ  ์‚ฌ์ดํŠธ reakwon.tistory.com/90

 

๋ฐ˜์‘ํ˜•