๊ฐ์
์ฝ๋ฉ ํ ์คํธ ์คํฐ๋๋ฅผ ํ๋ค๊ฐ String ํด๋์ค์ replace() ๋ฉ์๋์ replaceAll() ์ ์ฐฉ๊ฐํด์ ๊ธ์ ์ฐ๊ฒ ๋๋ค.
https://school.programmers.co.kr/learn/courses/30/lessons/17683
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
ํด๋น ๋ฌธ์ ์์ ๊ธ์๋ฅผ ๋ค๋ฅธ ๊ธ์๋ก ๋ฐ๊ฟ์ผํ๋ ์ผ์ด ์์๋๋ฐ, ํด๋นํ๋ ๋ชจ๋ ๊ธ์๋ฅผ ๋ค๋ฅธ ๊ธ์๋ก ๋ฐ๊ฟ์ผํ๋ค. ๋น์ฐํ replace ๊ด๋ จ ํจ์๋ฅผ ์๊ธฐ์ replaceAll() ์ ์จ์ ํ์ผ ๋ฌธ์์ด์ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ค๊ณ ํ๋ค.
ํ์๊ณผ ํ์ด ๊ณต์ ๋ฅผ ํ๋๋ฐ, ํ์์ replaceAll() ์ ์ฐ์ง ์๊ณ replace() ์ ์ฌ์ฉํ๋ค.
replace() ํจ์๋ ํ๊ฒ์ ํด๋นํ๋ ์ฒซ๋ฒ ์งธ ๋ฑ์ฅํ๋ ๋ฌธ์์ด๋ง ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ์ค ์์๊ณ , ๋ ๋ค๋ฅธ ํ์ ๋ํ ๋์ ๊ฐ์ด ์๊ฐํ๊ณ ์์๋ค.
ํ์ง๋ง, replace() ํจ์๋ ํ์ผ์ ํด๋นํ๋ ๋ฌธ์์ด์ ๋ชจ๋ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ธฐ๋ฅ์ ํ๋ค.
๊ทธ๋ ๋ค๋ฉด ๋ค๋ฅธ ์ ์ ๋ฌด์์ผ๊น?
replace() vs replaceAll()
String ํด๋์ค์ replace ํจ์์ replaceAll ํจ์๋ ๋ชจ๋ ๋ฌธ์์ด ๋ด์์ ํน์ ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฌธ์์ด๋ก ๋์ฒดํ๋ ์ญํ ์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ ํจ์ ์ฌ์ด์๋ ์ค์ํ ์ฐจ์ด๊ฐ ์๋ค.
๋จผ์ ์์ ๋ฅผ ํ๋ ๋ง๋ค์ด์ ๊ด๋ จ ๋ฉ์๋๋ฅผ ์ดํด ๋ณด์๋ค.
replace() ๋ฉ์๋
- replace(char oldChar, char newChar): ๋ฌธ์์ด ๋ด์์ ์ง์ ๋ ๋ฌธ์ oldChar๋ฅผ ์๋ก์ด ๋ฌธ์ newChar๋ก ๋์ฒดํ๋ค.
- replace(CharSequence target, CharSequence replacement): ๋ฌธ์์ด ๋ด์์ ์ง์ ๋ ๋ฌธ์ ์ํ์ค target์ ์๋ก์ด ๋ฌธ์ ์ํ์ค replacement๋ก ๋์ฒดํ๋ค.
- ์ด ๋ฉ์๋๋ค์ ์ ๊ท ํํ์์ ์ฌ์ฉํ์ง ์์ผ๋ฉฐ, ๋จ์ํ ๋ฌธ์์ด์ ์ํํ๋ฉด์ ํน์ ๋ฌธ์๋ ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฌธ์๋ ๋ฌธ์์ด๋ก ๊ต์ฒดํ๋ค.
์๋ฐ 11 ๊ธฐ์ค ๋ด๋ถ ๊ตฌํ
1
2
3
4
5
6
7
8
9
10
|
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
String ret = isLatin1() ? StringLatin1.replace(value, oldChar, newChar)
: StringUTF16.replace(value, oldChar, newChar);
if (ret != null) {
return ret;
}
}
return this;
}
|
cs |
๋ด๋ถ์์ ํธ์ถํ๋ replace ๋ฉ์๋๋ ๋ฐ์ดํธ ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด ์กฐ์์ ์ํํ๋ฉฐ, ํน์ ๋ฌธ์๋ฅผ ๋ค๋ฅธ ๋ฌธ์๋ก ๊ต์ฒดํ๋ค. ํ๋ผ๋ฏธํฐ์ ์๋ value ๋ณ์๊ฐ ์ธ์คํด์ค ๋ณ์์ธ byte[] ์ด๋ค.
LATIN1 ์ธ์ฝ๋ฉ๊ณผ UTF-16 ์ธ์ฝ๋ฉ์ ๋ชจ๋ ๊ณ ๋ คํ์ฌ ์ฒ๋ฆฌํ๊ธฐ ์ํจ์ผ๋ก ๋ฌธ์์ด์ ๋ฐ์ดํธ ๋ฐ์ดํฐ๋ก ๋ณํํ์ฌ ๋ฌธ์์ด ์กฐ์์ ํ๋ ๋ฐฉ์์ด๋ค. ์ธ์ฝ๋ฉ ๋ฐฉ์์๋ ์ฌ๋ฌ ๋ฐฉ์์ด ์๋ฏ์ด ์ธ์ฝ๋ฉ ๋ฐฉ์์ ๋ง์ถฐ ๋ฌธ์์ด์ ๋ณํํ๊ธฐ ์ํจ์ด๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public String replace(CharSequence target, CharSequence replacement) {
String tgtStr = target.toString();
String replStr = replacement.toString();
int j = indexOf(tgtStr);
if (j < 0) {
return this;
}
int tgtLen = tgtStr.length();
int tgtLen1 = Math.max(tgtLen, 1);
int thisLen = length();
int newLenHint = thisLen - tgtLen + replStr.length();
if (newLenHint < 0) {
throw new OutOfMemoryError();
}
StringBuilder sb = new StringBuilder(newLenHint);
int i = 0;
do {
sb.append(this, i, j).append(replStr);
i = j + tgtLen;
} while (j < thisLen && (j = indexOf(tgtStr, j + tgtLen1)) > 0);
return sb.append(this, i, thisLen).toString();
}
|
cs |
์ฌ์ฉ ์์
class Main {
public static void main(String[] args) {
String str = "hello world";
String result = str.replace('o', 'a');
System.out.println(result); // "hella warld"
String str2 = "hello world";
String result2 = str2.replace("world", "Java");
System.out.println(result2); // "hello Java"
}
}
replaceAll() ๋ฉ์๋
- replaceAll(String regex, String replacement): ๋ฌธ์์ด ๋ด์์ ์ง์ ๋ ์ ๊ท ํํ์ regex์ ์ผ์นํ๋ ๋ชจ๋ ๋ถ๋ถ์ ์๋ก์ด ๋ฌธ์์ด replacement๋ก ๋์ฒดํ๋ค.
- ์ด ๋ฉ์๋๋ ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ๋ ๋ณต์กํ ๋์ฒด ์์ ์ ์ํํ ์ ์๋ค.
์๋ฐ 11 ๊ธฐ์ค ๋ด๋ถ ๊ตฌํ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
public String replaceAll(String replacement) { //์์์ ํธ์ถํ replaceAll()
reset();
boolean result = find();
if (result) {
StringBuilder sb = new StringBuilder();
do {
appendReplacement(sb, replacement);
result = find();
} while (result);
appendTail(sb);
return sb.toString();
}
return text.toString();
}
|
cs |
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด replaceAll์ Pattern.compile(regex)๋ฅผ ํธ์ถํ์ฌ ์ ๊ท ํํ์์ ์ปดํ์ผํ ํ, Matcher ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ replaceAll ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค. replaceAll ๋ฉ์๋๋ ์ ๊ท ํํ์๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ถ๋ถ์ ๋์ฒด ๋ฌธ์์ด๋ก ๋ฐ๊พผ๋ค.
์ฌ์ฉ์์
class Main {
public static void main(String[] args) {
String str = "hello world";
String result = str.replaceAll("o", "a");
System.out.println(result); // "hella warld"
String str2 = "hello123world";
String result2 = str2.replaceAll("\\d", "X"); // ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ์ซ์๋ฅผ ๋์ฒด
System.out.println(result2); // "helloXXXworld"
}
}
์ฑ๋ฅ์ฐจ์ด
- replace ๋ฉ์๋์ ์ฑ๋ฅ
- ๋จ์ํ ๋ฌธ์์ด์ ์ํํ๋ฉด์ ํน์ ๋ฌธ์๋ ๋ฌธ์์ด์ ๋ค๋ฅธ ๋ฌธ์๋ ๋ฌธ์์ด๋ก ๊ต์ฒดํ๋ค.
- ์ ๊ท ํํ์์ ์ปดํ์ผํ๊ฑฐ๋ ๋งค์นญํ ํ์๊ฐ ์์ผ๋ฏ๋ก, ๋งค์ฐ ํจ์จ์ ์ด๋ค.
- ์๊ฐ ๋ณต์ก๋๋ O(n)์ด๋ค.
- replaceAll ๋ฉ์๋์ ์ฑ๋ฅ
- ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด์ ๋งค์นญํ๊ณ ๋์ฒดํ๋ค.
- ์ ๊ท ํํ์์ ์ปดํ์ผํ๊ณ ๋งค์นญํ๋ ๊ณผ์ ์ด ์ถ๊ฐ๋๋ฏ๋ก, ์ผ๋ฐ์ ์ผ๋ก ๋ ๋ง์ ์๊ฐ์ด ์์๋๋ค.
- ์ ๊ท ํํ์์ ๋งค์นญํ๋ ๊ณผ์ ์ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ณต์ก๋๊ฐ ๋ค๋ฅด๋ค. ์ผ๋ฐ์ ์ผ๋ก O(n) ์ด์์ ๋ณต์ก๋๋ฅผ ๊ฐ์ง ์ ์๋ค.
+ ) replaceFirst()
replaceFirst(String regex, String replacement): ๋ฌธ์์ด ๋ด์์ ์ง์ ๋ ์ ๊ท ํํ์ regex์ ์ผ์นํ๋ ์ฒซ ๋ฒ์งธ ๋ถ๋ถ์ ์๋ก์ด ๋ฌธ์์ด replacement๋ก ๋์ฒดํ๋ค. replaceAll๊ณผ ์ ์ฌํ์ง๋ง ์ฒซ ๋ฒ์งธ ์ผ์น๋ง ๋์ฒดํ๋ค๋ ์ ์ด ๋ค๋ฅด๋ค.
์๋ฐ 11 ๊ธฐ์ค ๋ด๋ถ ๊ตฌํ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
public String replaceFirst(String replacement) { // ํธ์ถํ replaceFirst() ๋ฉ์๋์ด๋ค.
if (replacement == null)
throw new NullPointerException("replacement");
reset();
if (!find())
return text.toString();
StringBuilder sb = new StringBuilder();
appendReplacement(sb, replacement);
appendTail(sb);
return sb.toString();
}
|
cs |
์ฌ์ฉ์์
public class Main {
public static void main(String[] args) {
String str = "hello123world456";
String result = str.replaceFirst("\\d", "X"); // ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ์ฒซ ๋ฒ์งธ ์ซ์๋ง ๋์ฒด
System.out.println(result); // "helloX23world456"
}
}
๋๊ธ