Language/Java

โ˜• ์ž๋ฐ” split / StringTokenizer - ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ ๋น„๊ต

์ธํŒŒ_ 2022. 11. 3. 12:55

java-split-StringTokenizer

์ž๋ฐ” ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ

์ž๋ฐ”์—์„œ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ํด๋ž˜์Šค๊ฐ€ String ํด๋ž˜์Šค์˜ split ๋ฉ”์„œ๋“œ์™€ StringTokenizer ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค.

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ด ๋‘˜์˜ ์‚ฌ์šฉ ๋ฌธ๋ฒ•์„ ์•Œ์•„๋ณด๊ณ , ๋‘˜์ด ์–ด๋– ํ•œ ์ฐจ์ด์ ์ด ์žˆ๋Š”์ง€, ์–ด๋–จ๋•Œ ์–ด๋Š ๊ฒƒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.


String ํด๋ž˜์Šค์˜ split ๋ฉ”์„œ๋“œ

String ํด๋ž˜์Šค์—์„œ ์ œ๊ณต๋˜๋Š” split ๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ 2๊ฐœ๋กœ ์˜ค๋ฒ„๋กœ๋”ฉ ๋˜์–ด ์žˆ๋‹ค.

์ž๋ฐ”์˜ split ๋ฉ”์„œ๋“œ์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ๊ตฌ๋ถ„์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ›๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ •๊ทœ์‹์œผ๋กœ ๋ฐ›๋Š”๋‹ค๋Š” ์ ์ด๋‹ค.

์ •๊ทœ์‹์ด ๋‚œ์ด๋„๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์ด๊ธด ํ•˜์ง€๋งŒ ๋•๋ถ„์— ๋‹ค์ฑ„๋กญ๊ณ  ์„ญ์„ธํ•˜๊ฒŒ ๋ฌธ์ž์—ด์„ ์ž๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

public String[] split(String regex);
// ๋ฐ˜ํ™˜์„ String ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค.
// ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ๋ฐ›๋Š”๋‹ค. (์ค‘์š”)

public String[] split(String regex, int limit);
// ๋ฌธ์ž์—ด์„ ์ •๊ทœ์‹์— ๋งž์ถฐ์„œ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ limit๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž๋ฅธ๋‹ค.
๋งŒ์ผ split์˜ ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ ๋™์ž‘์ด ์‹คํŒจํ•˜๋ฉด PatternSyntaxException ์˜ˆ์™ธ(์ •๊ทœ์‹ ํŒจํ„ด ์˜ค๋ฅ˜)๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

 

๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ

String str = "inpa@tistory@com@super@power";
String[] splitter = str.split("@");

for (int i=0; i < splitter.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter[i]);
}

java-string-split

 

String str = "inpa@tistory@com@super@power";
// ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋˜, ๋”ฑ 3๋ฒˆ ๊นŒ์ง€๋งŒ ์ž๋ฅด๊ณ  ๋ง์•„๋ผ
String[] splitter = str.split("@", 3); // limit ์‚ฌ์šฉ

for (int i=0; i < splitter.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter[i]);
}

java-string-split

 

์—ฌ๋Ÿฌ๊ฐœ ๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ

์ •๊ทœ์‹ or๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ๊ตฌ๋ถ„์ž๋กœ ์‚ฌ์šฉํ•  ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

String str = "hello-world%inpa@tistory#com";
String[] splitter = str.split("[%-@#]");

for (int i=0; i < splitter.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter[i]);
}

java-string-split

 

๊ตฌ๋ถ„์ž์— ์‚ฌ์šฉํ•  ํŠน์ • ๊ธฐํ˜ธ ์ฃผ์˜์ 

split ๋ฉ”์†Œ๋“œ๋Š” ์ •๊ทœํ‘œํ˜„์‹์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์—, ์ •๊ทœ์‹์œผ๋กœ ์ด๋ฏธ ์ง€์ •๋œ ์•ฝ์† ๊ธฐํ˜ธ๋ฅผ ์•„๋ฌด ์ƒ๊ฐ์—†์ด ์จ๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด | ๊ธฐํ˜ธ๋Š” ์ •๊ทœ์‹์—์„œ or์„ ์˜๋ฏธํ•˜๋Š”๋ฐ ๋ฌดํ„ฑ๋Œ€๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค.

String str = "inpa|tistory|com|super|power";
String[] splitter = str.split("|");

for (int i=0; i < splitter.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter[i]);
}

java-string-split

 

๋”ฐ๋ผ์„œ \\ ๋กœ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌํ•ด์„œ ์ธ์ž๋กœ ์ „๋‹ฌ ํ•ด์•ผ ํ•œ๋‹ค.

String str = "inpa|tistory|com|super|power";
String[] splitter = str.split("\\|"); // ๋ฌธ์ž | ๋กœ ์ž๋ฅด๊ธฐ

for (int i=0; i < splitter.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter[i]);
}

System.out.println("\n---------------------------------\n");

String str2 = "inpa.tistory.com.super.power";
String[] splitter2 = str2.split("\\."); // ๋ฌธ์ž . ๋กœ ์ž๋ฅด๊ธฐ

for (int i=0; i < splitter2.length; i++) {
    System.out.printf("%d์œ„์น˜ : %s\n", i, splitter2[i]);
}

java-string-split


StringTokenizer ํด๋ž˜์Šค

StringTokenizer ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„์ž(delimiter)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐ(token)์ด๋ผ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ž˜๋ผ๋‚ด๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

๋งŒ์ผ ๊ตฌ๋ถ„์ž๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์œผ๋กœ ๊ณต๋ฐฑ์œผ๋กœ ์„ค์ •๋˜์–ด ๋ฌธ์ž์—ด์„ ์ž๋ฅด๊ฒŒ ๋œ๋‹ค.

// ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์ž๋ฅด๊ธฐ
new StringTokenizer(String str)


// ๋ฌธ์ž์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •๋œ ๊ตฌ๋ถ„์ž(delim)๋กœ ์ž๋ฅด๊ธฐ
// ์ด๋•Œ ๊ตฌ๋ถ„์ž๋Š” ํ† ํฐ์œผ๋กœ ๊ฐ„์ฃผ๋˜์ง€ ์•Š์Œ
new StringTokenizer(String st, String delim)


// ๋ฌธ์ž์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •๋œ ๊ตฌ๋ถ„์ž(delim)๋กœ ์ž๋ฅด๊ธฐ
// returnDelims ์˜ ๊ฐ’์„ true๋กœํ•˜๋ฉด ๊ตฌ๋ถ„์ž๋„ ํ† ํฐ์œผ๋กœ ๊ฐ„์ฃผ
new StringTokenizer(String str, String delim, boolean returnDelims)

 

์œ„์˜ ๊ณผ์ •์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฒฐ๊ณผ๋ฌผ์€ ๋ฐฐ์—ด์ด ์•„๋‹Œ, ๊ตฌ๋ถ„์ž์— ๋”ฐ๋ผ ๋‚˜๋ˆ„์–ด์ง„ ๋ฌธ์ž์—ด์˜ ํ† ํฐ์„ ๊ฐ–๊ณ  ์žˆ๊ฒŒ ๋œ๋‹ค.

๊ฐœ๋ฐœ์ž๋Š” ์ด๋Ÿฌํ•œ ํ† ํฐ์„ ๊บผ๋‚ด์™€์„œ StringTokenizer์—์„œ ์ง€์›ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ž์—ด์„ ๊ฐ€๊ณตํ•ด์•ผ ํ•˜๋Š” ์‹์œผ๋กœ split ๊ณผ๋Š” ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

StringTokenizer ๋ฉ”์„œ๋“œ ์„ค ๋ช…
int countTokens( ) ์ „์ฒด ํ† ํฐ์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
boolean hasMoreTokens( )
boolean hasMoreElements()
ํ† ํฐ์ด ๋‚จ์•„์žˆ๋Š”์ง€ ์•Œ๋ ค์คŒ
String nextToken( ) ๋‹ค์Œ ํ† ํฐ์„ ๋ฐ˜ํ™˜

 

๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ 

// 1. delim์„ default๋กœ ์„ค์ •.
String str1 = "Wellcome to The Java HelloWorld";
StringTokenizer st = new StringTokenizer(str1);

System.out.println("- str1์˜ ํ† ํฐ ๊ฐœ์ˆ˜ : " + st.countTokens());
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}
// 2. delim์„ '%'๋กœ ์„ค์ •.
String str2 = "Wellcome%to%The%Java%HelloWorld";
StringTokenizer st = new StringTokenizer(str2, "%");

System.out.println("- str2์˜ ํ† ํฐ ๊ฐœ์ˆ˜ : " + st.countTokens());
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}

java-StringTokenizer

 

์—ฌ๋Ÿฌ๊ฐœ ๊ตฌ๋ถ„์ž๋กœ ๋ฌธ์ž์—ด ๋ถ„๋ฆฌํ•˜๊ธฐ 

๋งŒ์ผ ๊ตฌ๋ถ„์ž๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ธ์‹์‹œ์ผœ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๊ณ  ์‹ถ๋‹ค๋ฉด "%/*" ์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ธฐ์ค€์„ ํ•œ ๋ฒˆ์— ๋„ฃ์–ด ์ฃผ๋ฉด ๋œ๋‹ค.

๋‹จ, ๊ธฐ์ค€์€ ๋ฌด์กฐ๊ฑด 1์ž๋ฆฌ ๋ฌธ์ž์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์ ์„ ์ฃผ์˜ํ•˜์ž.

// 3. delim์„ '%', '/', '*'๋กœ ์„ค์ •.
String str3 = "Wellcome%to/The%Java*HelloWorld";
StringTokenizer st = new StringTokenizer(str3, "%/*");

System.out.println("- str3์˜ ํ† ํฐ ๊ฐœ์ˆ˜ : " + st.countTokens());
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}

java-StringTokenizer

 

๊ตฌ๋ถ„์ž๋„ ํ† ํฐ์œผ๋กœ ํฌํ•จ์‹œ์ผœ ๋ถ„๋ฆฌํ•˜๊ธฐ

๊ตฌ๋ถ„์ž๋ฅผ ๋ฌธ์ž์—ด ์ž๋ฅด๋Š”๋ฐ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ† ํฐ์œผ๋กœ๋„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

String str4 = "Wellcome%to%The%Java%HelloWorld";
// %๋ฅผ ๊ตฌ๋ถ„์ž๋กœ์„œ ์—ญํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ถœ๋ ฅ๋˜๋„๋ก ์„ค์ •
StringTokenizer st = new StringTokenizer(str4, "%", true); // 3๋ฒˆ์งธ ์ธ์ž๋ฅผ true๋กœ

System.out.println("- str4์˜ ํ† ํฐ ๊ฐœ์ˆ˜ : " + st.countTokens());
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}

java-StringTokenizer

 

๊ตฌ๋ถ„์ž ์ค‘๊ฐ„์— ์žฌ์„ค์ •ํ•˜๊ธฐ

StringTokenizer์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ์ค‘๊ฐ„์— ๊ตฌ๋ถ„์ž๋ฅผ ์ƒˆ๋กœ ์„ค์ • ํ• ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

// 4. delim์„ '%'์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋‹ค๊ฐ€, delim์„ '&'๋กœ ์žฌ์„ค์ •ํ•˜๊ณ  ์ถœ๋ ฅ.
String str4 = "Wellcome%%to%&The&Java&HelloWorld";
StringTokenizer st = new StringTokenizer(str4, "%");

System.out.println("- str4์˜ ํ† ํฐ ๊ฐœ์ˆ˜ (delim : '%') : " + st.countTokens());
System.out.println(st.nextToken());
System.out.println(st.nextToken());

System.out.println(st.nextToken("&")); // ๊ตฌ๋ถ„์ž ์žฌ์„ค์ •

System.out.println("- str4์˜ ํ† ํฐ ๊ฐœ์ˆ˜ (delim : '&') : " + st.countTokens());
System.out.println(st.nextToken());
System.out.println(st.nextToken());
System.out.println(st.nextToken());

java-StringTokenizer


Split vs StringTokenizer ์ฐจ์ด์ 

  • ๋จผ์ € split ๋ฉ”์†Œ๋“œ๋Š” Stringํด๋ž˜์Šค์— ์†ํ•ด์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ด๊ณ , StringTokenizer๋Š” java.util์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค.
  • ๊ตฌ๋ถ„์ž๋ฅผ split๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ , StringTokenizer๋Š” ๋ฌธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.
  • split๋Š” ๊ฒฐ๊ณผ ๊ฐ’์ด ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด์ง€๋งŒ, stringtokenizer๋Š” ๊ฐ์ฒด์ด๋‹ค.
  • split๋Š” ๋นˆ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๋ฐ˜๋ฉด, StringTokenizer๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๋Š”๋‹ค. (๋’ค์— ์ฝ”๋“œ ์˜ˆ์ œ)
  • ์„ฑ๋Šฅ์€ split ๋ณด๋‹ค StringTokenizer ๊ฐ€ ์ข‹๋‹ค.
    split์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ† ํฐ์œผ๋กœ ์ž˜๋ผ๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— StringTokenizer ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง„๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์€ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋ณ„ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

๋‹ค์Œ์€ ๊ตฌ๋ถ„์ž๋ฅผ , ๋กœ ํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ  String ํด๋ž˜์Šค์˜  split() ๊ณผ  StringTokenizer ๋กœ ์ž˜๋ผ๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.  

split() ์€ ๋นˆ ๋ฌธ์ž์—ด๋„ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜๋Š” ๋ฐ˜๋ฉด,  StringTokenizer๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ† ํฐ ๊ฐœ์ˆ˜์— ์ฐจ์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

String data = "100,,,200,300";

// split์€ ๋นˆ๋ฌธ์ž ๊นŒ์ง€ ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.
String[] result = data.split(",");
for (int i = 0; i < result.length; i++) {
    System.out.print(result[i] + "|"); // ๋นˆ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด | ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅ
}
System.out.printf("%nsplit()์˜ ๊ฐœ์ˆ˜ : " + result.length + "%n%n");


// StringTokenizer ๋นˆ๋ฌธ์ž๋Š” ํ† ํฐ์œผ๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๋Š”๋‹ค.
StringTokenizer st = new StringTokenizer(data, ",");
int countTokens = st.countTokens(); // ์ „์ฒด ํ† ํฐ์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
for (int j = 0; st.hasMoreElements(); j++) {
    System.out.print(st.nextToken() + "|"); // ๋นˆ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด | ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅ
}
System.out.printf("%nStringTokenizer๊ฐœ์ˆ˜ : " + countTokens);

java-split-StringTokenizer