replace ๐Ÿ†š replaceAll

nacSeo (๋‚™์„œ)ยท2022๋…„ 11์›” 20์ผ
0

์ง€์‹์ €์žฅ์†Œ

๋ชฉ๋ก ๋ณด๊ธฐ
2/34

StringifyJSON ๊ณผ์ œ ๋„์ค‘ ์‹์€ ๋‹ค ๋งž๋Š”๋ฐ, ๋นˆ์นธ์ด ๊ณต๋ฐฑ์œผ๋กœ ํ‘œ์‹œ๋˜๊ฑฐ๋‚˜ :๋กœ ํ‘œ์‹œ๋˜์–ด์•ผ ํ•  ๊ฒŒ =๋กœ ํ‘œ์‹œ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ๋‹ค.
์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ๋ฌธ์ž์—ด์„ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ธ replace, replaceAll ๋ฉ”์„œ๋“œ๋‹ค.
ํ•ด๋‹น ๊ณผ์ œ์—์„œ๋Š” ๋‘˜ ๋‹ค ์ž…๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๋‚˜์™€ ์–ธ๋“ฏ ๋ณด๊ธฐ์—” '๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹๊นŒ?' ํ•˜์ง€๋งŒ ๋ถ„๋ช… ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค.

String replace(CharSequnce target, CharSequence replacement)

String replaceAll(String regex, String replacement)

์œ„์™€ ๊ฐ™์ด relace์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ์ธ์ž๊ฐ’์ด ๋ฌธ์ž์—ด๋กœ ๋“ค์–ด๊ฐ€๊ธฐ์— ๋ฌธ์ž์—ด๋งŒ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ,
replaceAll์€ ์ธ์ž๊ฐ’์— ์ •๊ทœ์‹์ด ๋“ค์–ด๊ฐ€๋ฉด์„œ ๋ถˆํŠน์ • ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด,

String str = "abacabcbcdeabcccdde";

String result1 = str.replace("abc", "์ผ€");
String result2 = str.replaceAll("[abc]", "์ผ€");

System.out.println("replace result : " + result1);
System.out.println("replaceAll result : " + result2);

--------------------------------------------------------
// ๊ฒฐ๊ณผ๊ฐ’
replace result : abac์ผ€bcde์ผ€ccdde
replaceAll result : ์ผ€์ผ€์ผ€์ผ€์ผ€์ผ€์ผ€์ผ€์ผ€de์ผ€์ผ€์ผ€์ผ€์ผ€dde

์ด์™€ ๊ฐ™์ด, replace๋Š” ํ•ด๋‹น ๋ฌธ์ž์—ด ๊ทธ ์ž์ฒด๋งŒ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ์— ๋น„ํ•ด
replaceAll์€ ์ •๊ทœ์‹ [ ]์•ˆ์— ์กด์žฌํ•˜๋Š” a,b,c๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ๋ณ€ํ™˜ํ•ด์ค€๋‹ค๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค€๋‹ค.

profile
๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž ๊น€์ฐฝํ•˜์ž…๋‹ˆ๋‹ค ๐Ÿ™‡โ€โ™‚๏ธ

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