๐ค ๋ฌธ์ ์์
ํ์ ์ฝ๋ฉ ํ ์คํธ ์ฐ์ต์ ํ๋ค ๋ณด๋ฉด split์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
ํ์ง๋ง ๋๋๋ก ์ํ๋ ๋ฐํ๊ฐ์ด ์๋ ์์์ธ์ ๊ฐ์ด ๋์๊ณ , ์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๊ฐ์ข ๋ถ๊ธฐ๋ฌธ์ ์ถ๊ฐํ ์ ์ด ์๋ค.
์ ๊ทธ๋ฐ ๊ฒ์ธ์ง ์ ๋ฆฌํ๊ณ , ๊น๋ํ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์ถ์๋ค.
๐ String.split()
public String[] split(String regex) {
return split(regex, 0, false);
}
private String[] split(String regex, int limit, boolean withDelimiters) {
/* fastpath if the regex is a
* (1) one-char String and this character is not one of the
* RegEx's meta characters ".$|()[{^?*+\\", or
* (2) two-char String and the first char is the backslash and
* the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.length() == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
// All the checks above can potentially be constant folded by
// a JIT/AOT compiler when the regex is a constant string.
// That requires method inlining of the checks, which is only
// possible when the actual split logic is in a separate method
// because the large split loop can usually not be inlined.
return split(ch, limit, withDelimiters);
}
Pattern pattern = Pattern.compile(regex);
return withDelimiters
? pattern.splitWithDelimiters(this, limit)
: pattern.split(this, limit);
}
private String[] split(char ch, int limit, boolean withDelimiters) {
int matchCount = 0;
int off = 0;
int next;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
String del = withDelimiters ? String.valueOf(ch) : null;
while ((next = indexOf(ch, off)) != -1) {
if (!limited || matchCount < limit - 1) {
list.add(substring(off, next));
if (withDelimiters) {
list.add(del);
}
off = next + 1;
++matchCount;
} else { // last one
int last = length();
list.add(substring(off, last));
off = last;
++matchCount;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[] {this};
// Add remaining segment
if (!limited || matchCount < limit)
list.add(substring(off, length()));
// Construct result
int resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
resultSize--;
}
}
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
์๊ฐ๋ณด๋ค ๋ฐฉ๋ํ ์์ ์ฝ๋๊ฐ ์กด์ฌํ๋ค.
์ฐ๋ฆฌ๊ฐ ์ฃผ์ํ๊ฒ ์์์ผ ํ ๊ฒ์ limit์ด๋ค.
1๏ธโฃ ๊ธฐ๋ณธ ๋ฌธ๋ฒ
String[] result = str.split(String regex);
String[] result = str.split(String regex, int limit);
- regex
- split์ ๋จ์ ๋ฌธ์๊ฐ ์๋๋ผ ์ ๊ทํํ์(regular expression)์ผ๋ก ๋์ํ๋ค.
- ., |, *, ? ๊ฐ์ ๋ฉํ๋ฌธ์๋ ์ด์ค์ผ์ดํ(\\)ํด์ผ ๋ฌธ์ ๊ทธ๋๋ก ์ธ์๋๋ค.
-
"a.b".split("\\.") // ["a", "b"]
- limit
- ๋๋ ์ต๋ ๊ฐ์ ๋๋ ๋น ๋ฌธ์์ด ์ฒ๋ฆฌ ๋ฐฉ์์ ์ํฅ์ ์ค๋ค.
2๏ธโฃ limit ๊ฐ์ ๋ฐ๋ฅธ ๋์
| > 0 | ์ต๋ limit๊ฐ์ ์์ ๋ฐํ. ๋ง์ง๋ง ์์์๋ ๋๋จธ์ง ๋ฌธ์์ด ์ ๋ถ ํฌํจ. |
| 0 (๊ธฐ๋ณธ๊ฐ) | ๋ฐฐ์ด ๋๋ถ๋ถ์ ๋น ๋ฌธ์์ด์ ์ ๋ถ ์ ๊ฑฐ ํ ๋ฐํ. → ํจํด์ด ๋งจ ๋์ ์์ผ๋ฉด ๋น ๋ฌธ์์ด์ด ์ฌ๋ผ์ง |
| < 0 | ๋ฐฐ์ด ๋์ ๋น ๋ฌธ์์ด๋ ํฌํจํ์ฌ ๋ชจ๋ ๋ฐํ. → ๋งจ ์/๋ค ํจํด๋ ๋์น์ง ์์ |
3๏ธโฃ ๋์ ์์
์์ 1 : ๊ธฐ๋ณธ(0)
"EWEW".split("EW") // []
"EWEWE".split("EW") // ["", "", "E"]
- "EWEW" → ๋ง์ง๋ง์ด "EW"๋ก ๋๋์ ๋น ๋ฌธ์์ด๋ง ๋จ์ → ์ ๊ฑฐ๋จ → ๊ธธ์ด 0
- "EWEWE" → ๋ง์ง๋ง์ด "E"๋ก ๋๋์ ๋น ๋ฌธ์์ด ์๋ → ์ ์ง๋จ
์์ 2 : limit = -1
"EWEW".split("EW", -1) // ["", "", ""]
"EWEWE".split("EW", -1) // ["", "", "E"]
- ๋น ๋ฌธ์์ด๋ ์ ์ง → ํญ์ "EW" ๊ฐ์๋ (๋ฐฐ์ด ๊ธธ์ด - 1)
์์ 3 : ์ ๊ท์ ์ฃผ์
"a|b|c".split("\\|") // ["a", "b", "c"]
"a|b|c".split("|") // ["a", "|", "b", "|", "c"] // ์๋ชป๋ ์ฌ์ฉ, '|'๋ OR ์ฐ์ฐ์
4๏ธโฃ ํจํด ๊ฐ์ ์ธ๊ธฐ ๊ณต์
- ๊ฐ์ = split(regex, -1).length - 1
- limit=-1์ด๋ฉด ์๋ค ํจํด์ด ์์ด๋ ๋น ๋ฌธ์์ด์ด ์ ๊ฑฐ๋์ง ์์ ์ ํํ ๊ฐ์ ๊ณ์ฐ ๊ฐ๋ฅ
5๏ธโฃ withDelimiters์ ์ญํ
1. ๋๋ฆฌ๋ฏธํฐ์ ์๋ฏธ
- split("EW", ...)๋ผ๊ณ ํ๋ฉด "EW"๊ฐ ๋๋ฆฌ๋ฏธํฐ์ด๋ค.
- ๋ฌธ์์ด์ ์๋ผ๋ผ ๋ "EW"๊ฐ ๋ํ๋๋ ์์น๋ฅผ ๊ฒฝ๊ณ๋ก ๋๋๋๋ค"๋ผ๋ ๋ป์ด๋ค.
2. withDelimiters์ ์ญํ
Java ํ์ค String.split()์๋ withDelimiters ๋งค๊ฐ๋ณ์๊ฐ ์์ง๋ง,
์ด๊ฑด ์ฌ์ฉ์ ์ ์ ๋ฉ์๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ตฌํ์์ ์ถ๊ฐํ ์ต์
์ผ ๊ฐ๋ฅ์ฑ์ด ํฌ๋ค.
withDelimiters = true
→ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ๊ตฌ๋ถ์(๋๋ฆฌ๋ฏธํฐ)๋ ํฌํจ.
withDelimiters = false
→ ๊ฒฐ๊ณผ ๋ฐฐ์ด์๋ ๊ตฌ๋ถ์ ์์ด ์๋ฆฐ ๋ด์ฉ๋ง ํฌํจ.
(ํ์ค String.split()์ด ๋ฐ๋ก ์ด ๋ฐฉ์)
์์ (withDelimiters ๊ฐ๋ ์๋ฎฌ๋ ์ด์ )
String s = "EWEWE";
s.split("EW", -1) → ["", "", "E"]
s.splitWithDelimiters("EW", -1) → ["", "EW", "", "EW", "E"]
- withDelimiters=true์ธ ๊ฒฝ์ฐ "EW"๋ผ๋ ๊ตฌ๋ถ์ ์์ฒด๋ ๋ฐฐ์ด์ ๋ค์ด๊ฐ๋ค.
- ์ ๊ท์ ๋งค์นญ์ ์บก์ฒ ๊ทธ๋ฃน์ผ๋ก ์ก๊ณ ๊ฒฐ๊ณผ์ ํฌํจ์ํค๋ ๋ฐฉ์์ผ๋ก ๊ตฌํ ๊ฐ๋ฅ.
- split์ ์ฌ์ฉ ์ private splitWithDelimiters๋ฅผ false๋ก ํธ์ถํ๊ณ , public splitWithDelimiters ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ private splitWithDelimiters๋ฅผ true๋ก ํธ์ถ๋๋ค.
- ๋ฉ์๋๊ฐ ์๋ก ๋ค๋ฅด๋ค!
'์ง์ ์ ๋ฆฌ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [JAVA] HashMap์ ๋์์ฑ ๋ฌธ์ (2) | 2025.06.29 |
|---|---|
| [JAVA] java์ ๋๋ค๋? (2) | 2024.11.19 |
| Http Request, metod, header, response, MIME (0) | 2024.07.06 |
| ์ฐ๋ ๋, ๋๊ธฐ, ์ฐ๋ ๋ํ, Runnable, Callable, Future (0) | 2024.07.04 |
| Request Header ์ง์ ์ฝ๊ธฐ ๋ฐ ํ์ผ ์ฝ๊ธฐ (0) | 2024.07.03 |