๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] 1152. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

์ดํ•˜์–€ยท2024๋…„ 1์›” 31์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
2/37
post-thumbnail

โœ… Split

string.split("{๊ธฐ์ค€}")

์ž…๋ ฅ๋ฐ›์€ ์ •๊ทœํ‘œํ˜„์‹ ๋˜๋Š” ํŠน์ • ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด์— ์ €์žฅํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

string.split(" ")

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๊ณต๋ฐฑ์œผ๋กœ ์ž˜๋ฆฝ๋‹ˆ๋‹ค.
  • ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ ์‹œ, ๊ณต๋ฐฑ์˜ ๊ฐœ์ˆ˜๋Œ€๋กœ ์ž˜๋ฆฝ๋‹ˆ๋‹ค.
  • ๊ณต๋ฐฑ์ด ๋์— ์žˆ๋Š” ๊ฒฝ์šฐ, ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

string.split(" ", -1)

  • ์œ„์˜ 2๊ฐ€์ง€๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ, ๊ณต๋ฐฑ์œผ๋กœ ๋๋‚˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น๋˜๋Š” ๊ณต๋ฐฑ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋‘ ์…‰๋‹ˆ๋‹ค.

๐Ÿ”— https://gymdev.tistory.com/52



โœ… trim

string.trim()

๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ, ์–‘ ๋ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.



โœ… StringTokenizer

StringTokenizer stringTokenizer = new StringTokenizer(string, " ");
System.out.println(stringTokenizer.countTokens());

java.util.StringTokenizer ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ตฌ๋ถ„์ž(delimiter)๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ชผ๊ฐœ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
์ชผ๊ฐœ์ง„ ๋ฌธ์ž์—ด๋“ค์€ Token์œผ๋กœ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

  • ์ƒ์„ฑ์ž 3๊ฐ€์ง€

    ์ƒ์„ฑ์ž๋‚ด์šฉ
    StringTokenizer(String string)๋ฌธ์ž์—ด(string)์„ ๊ธฐ๋ณธ ๊ตฌ๋ถ„์ž(๋„์–ด์“ฐ๊ธฐ)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
    StringTokenizer(String str, String delimiter)๋ฌธ์ž์—ด(string)์„ ์ง€์ •๋œ ๊ตฌ๋ถ„์ž(delimiter)๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
    (๊ตฌ๋ถ„์ž์˜ ๊ฒฝ์šฐ Token์œผ๋กœ ๊ฐ„์ฃผ๋˜์ง€ ์•Š์Œ)
    StringTokenizer(String str, String delimiter, boolean returnDelims)๋ฌธ์ž์—ด(string)์„ ์ง€์ •๋œ ๊ตฌ๋ถ„์ž(delimiter)๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
    returnDelims์˜ ๊ฐ’์„ true๋กœ ํ•˜๋ฉด ๊ตฌ๋ถ„์ž๋„ ํ† ํฐ์œผ๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค.

  • ๋ฉ”์„œ๋“œ 6๊ฐ€์ง€

    ๋ฆฌํ„ด๊ฐ’๋ฉ”์„œ๋“œ๋‚ด์šฉ
    StringnextToken()๊ฐ์ฒด์—์„œ ๋‹ค์Œ Token์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    StringnextToken(String delimiter)delimiter๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ Token์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    intcountTokens()์ „์ฒด Token์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    booleanhasMoreTokens()Token์ด ๋‚จ์•„์žˆ๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.
    booleanhasMoreElements()hasMoreToken๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ์ง€๋งŒ, Element ๋ณด๋‹ค๋Š” ์ฃผ๋กœ Token ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    ObjectnextElement()nextToken ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•˜์ง€๋งŒ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

โžก๏ธ 1152๋ฒˆ ๋ฌธ์ œ ํ’€์ด์— intํ˜•์˜ countTokens()๋ฅผ ์ด์šฉํ•ด Token์˜ ์ˆ˜(=๋‹จ์–ด ๊ฐœ์ˆ˜)๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๐Ÿ”— [Java] StringTokenizer์™€ Split() ๋ฉ”์„œ๋“œ ์–ธ์ œ ์จ์•ผํ• ๊นŒ?



โœ… Split VS StringTokenizer

SplitStringTokenizer
String ํด๋ž˜์Šค์— ์†ํ•ด์žˆ๋Š” ๋ฉ”์†Œ๋“œjava.util์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค
์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ๊ตฌ๋ถ„๋ฌธ์ž๋กœ ๋ฌธ์ž์—ด์„ ๊ตฌ๋ถ„
์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•˜๋ฉด ๋‘ ๋ฌธ์ž ์ด์ƒ์˜ ๊ตฌ๋ถ„์ž๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ์˜ค์ง ๋‹จ ํ•œ ๋ฌธ์ž์˜ ๊ตฌ๋ถ„์ž๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
๊ฒฐ๊ณผ๊ฐ’์ด ๋ฌธ์ž์—ด ๋ฐฐ์—ด String[]๊ฒฐ๊ณผ๊ฐ’์ด ๋ฌธ์ž์—ด String
๋นˆ ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ธ์‹๋นˆ ๋ฌธ์ž์—ด์„ ํ† ํฐ์œผ๋กœ ์ธ์‹ X

  • ๋ฐ์ดํ„ฐ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ๋ฅผ ์ „์ œ๋กœ ํ•  ๊ฒฝ์šฐ
    โžก๏ธ ์„ฑ๋Šฅ : StringTokenizer > Split
    • StringTokenizer(์œ„) : ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๋กœ ์ž˜๋ผ์„œ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋น ๋ฆ…๋‹ˆ๋‹ค.
    • Split(์•„๋ž˜) : ๋ฐ์ดํ„ฐ๋ฅผ ํ† ํฐ์œผ๋กœ ์ž˜๋ผ๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋Š๋ฆฝ๋‹ˆ๋‹ค.

  • ๋‹จ, ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์•„์ง„๋‹ค๋ฉด ๋‘ ๊ฐ€์ง€์˜ ์„ฑ๋Šฅ์€ ํฐ ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๐Ÿ”— [Java] StringTokenizer์™€ Split() ๋ฉ”์„œ๋“œ ์–ธ์ œ ์จ์•ผํ• ๊นŒ?
๐Ÿ”— ์ž๋ฐ” split / StringTokenizer - ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ ๋น„๊ต

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

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