[Java] ๐Ÿ’พ StringTokenizer

shrยท2022๋…„ 5์›” 25์ผ
0

Concept

๋ชฉ๋ก ๋ณด๊ธฐ
10/13
post-thumbnail

์‚ฌ์šฉ๋ฒ•


๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ธ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

import java.util.StringTokenizer;		/* 1. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ */

public class Main
{
	public static void main(String[] args)
    {
    	String Tokenizer st1 = new StringTokenizer("๋ฌธ์ž์—ด");			/* ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ */
        String Tokenizer st2 = new StringTokenizer("๋ฌธ์ž์—ด", "!!");		/* !!๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ */
        String Tokenizer st3 = new StringTokenizer("๋ฌธ์ž์—ด", "โ™ก", true/false)
        /* โ™ก๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ, โ™ก๋ฅผ ๋ฌธ์ž์—ด ํ† ํฐ์— ํฌํ•จ์‹œํ‚ฌ์ง€(true) ์ œ์™ธํ• ์ง€(false) - ๋””ํดํŠธ๋Š” false */
    }
}

StringTokenizer๋Š” java.util์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค๋ฏ€๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ ๋นˆ ๋ฌธ์ž์—ด์€ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๊ณ , ๊ฒฐ๊ณผ ๊ฐ’์ด ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ ์ „์ฒด ํ† ํฐ์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„ ์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.


๋ฉ”์†Œ๋“œ


import java.util.StringTokenizer;
public class Main
{
	public static void main(String[] args) 
	{
    	String str = "์‚ฌ๊ณผ ๋”ธ๊ธฐ ํ† ๋งˆํ† !! ์•ต๋‘ ๋ง๊ณ ";
        StringTokenizer st1 = new StringTokenizer(str);
        System.out.println(st1.countTokens());		/* 5 */
        
        while (st1.hasMoreTokens()) {					/* ๋‚จ์•„ ์žˆ๋Š” ํ† ํฐ์ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false ๋ฆฌํ„ด */
        	System.out.println(st1.nextToken());		/* ๊ฐ์ฒด์—์„œ ๋‹ค์Œ ํ† ํฐ์„ ๋ฐ˜ํ™˜ */
        }
        
        String str2 = "์‚ฌ๊ณผ ๋”ธ๊ธฐ, ํ† ๋งˆํ†  ์•ต๋‘!! ๋ง๊ณ ";
        StringTokenizer st2 = new StringTokenizer(str2, ",!!");
        /* ๊ตฌ๋ถ„์ž๋Š” ํ•œ ๊ฐœ ์ด์ƒ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. */
        while (st2.hasMoreTokens()) {
        	System.out.println(st2.nextToken());
        }
    }
}

์ฐธ๊ณ  ์ž๋ฃŒ


[JAVA ์ž๋ฐ”] StringTokenizer ํด๋ž˜์Šค๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ! split ๋น„๊ต.

profile
๋ชปํ•˜๋‹ค ๋ณด๋ฉด ์ž˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฑฐ์•ผ ๏ฝฅแด—๏ฝฅฬฅฬฅฬฅ

0๊ฐœ์˜ ๋Œ“๊ธ€