CodeGym /Java Course /All lectures for KO purposes /String ํด๋ž˜์Šค์˜ ๊ตฌ์กฐ

String ํด๋ž˜์Šค์˜ ๊ตฌ์กฐ

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 547
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"์•„๋ฏธ๊ณ , ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด? int ๋‹ค์Œ์œผ๋กœ ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ์ž๋ฐ” ํด๋ž˜์Šค๋Š” ๋ญ์•ผ?"

"์ด๋ฏธ ์ˆ˜์—… ์ œ๋ชฉ์— ์Šคํฌ์ผ๋Ÿฌ๋ฅผ ์ฃผ์…จ๊ตฐ์š”, ์—˜๋ฆฌ. ๋ฐ”๋กœ String!"

"์‹ค์ œ๋กœ, ๊ทธ๊ฒƒ์€ ์Šคํฌ์ผ๋Ÿฌ์ž…๋‹ˆ๋‹ค. String์ ˆ๋Œ€์ ์œผ๋กœ ๋ชจ๋“  ๊ณณ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์•Œ๊ณ  ์žˆ์œผ๋ฉด ๋” ์ข‹์€ ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋งŽ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

" Stringํด๋ž˜์Šค๋Š” ๋ฌธ์—์„œ ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ์œ ํ˜• ์ด์™ธ์˜ ์œ ์ผํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค switch. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋ฌธ์ž์—ด ์ถ”๊ฐ€ ๋ฐ ๋ฌธ์ž์—ด ๊ฐœ์ฒด๋ฅผ ํŠน๋ณ„ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. String๊ฐœ์ฒด๋Š” ํŠน๋ณ„ํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํด๋ž˜์Šค String๋Š” ๋งค์šฐ ํŠน๋ณ„ ์ˆ˜์—….

"๋˜ํ•œ ์ด Stringํด๋ž˜์Šค์—๋Š” Java์—์„œ ๋ฌธ์ž์—ด ์ž‘์—…์„ ๋”์šฑ ๋‹จ์ˆœํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•˜๋Š” ๋งŽ์€ ๋„์šฐ๋ฏธ ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋“  ๊ฒƒ์„ ๋ฐฐ์šฐ๋ฉด ๋งŽ์€ ์ž‘์—…์„ ํ›จ์”ฌ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

"๋‚˜๋Š” ๊ธฐ๋‹ค๋ฆด ์ˆ˜ ์—†์–ด."

"์Œ, ์šฐ๋ฆฌ๋Š” ์ด ์ƒํƒœ๊ณ„์˜ ํ•ต์‹ฌ์ธ ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค String. ํด๋ž˜์Šค์˜ ๊ตฌ์กฐ๋Š” ์‹ค์ œ๋กœ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค ๋‚ด๋ถ€์—๋Š” ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฌธ์ž ๋ฐฐ์—ด ( ๋ฐฐ์—ด) String์ด ์žˆ์Šต๋‹ˆ๋‹ค . ์˜ˆ๋ฅผ ๋“ค์–ด 'Hello'๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ €์žฅ๋˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.char

String ํด๋ž˜์Šค์˜ ๊ตฌ์กฐ

์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

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

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

์ด Stringํด๋ž˜์Šค์—๋Š” ๋งŽ์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž๋งŒ 18๊ฐœ์ž…๋‹ˆ๋‹ค! ๋”ฐ๋ผ์„œ ์•„๋ž˜์—๋Š” ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•ญ๋ชฉ๋งŒ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค.

ํ–‰๋™ ์–‘์‹ ์„ค๋ช…
int length()
๋ฌธ์ž์—ด์˜ ๋ฌธ์ž ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
boolean isEmpty()
๋ฌธ์ž์—ด์ด ๋นˆ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ
boolean isBlank()
๋ฌธ์ž์—ด์— ๊ณต๋ฐฑ, ํƒญ, ์ƒˆ ์ค„ ๋“ฑ์˜ ๊ณต๋ฐฑ ๋ฌธ์ž๋งŒ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
char charAt(int index)
๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
char[] toCharArray()
๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฌธ์ž(์‚ฌ๋ณธ)์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
byte[] getBytes()
๋ฌธ์ž์—ด์„ ๋ฐ”์ดํŠธ ์ง‘ํ•ฉ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
String[] split(String regex)
๋ฌธ์ž์—ด์„ ์—ฌ๋Ÿฌ ํ•˜์œ„ ๋ฌธ์ž์—ด๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.
String join(CharSequence delimiter, elements)
์—ฌ๋Ÿฌ ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ํ•จ๊ป˜ ๊ฒฐํ•ฉ
String intern()
๋ฌธ์ž์—ด์„ ๋ฌธ์ž์—ด ํ’€์— ๋„ฃ์Šต๋‹ˆ๋‹ค.

"๋งค์šฐ ์ข‹์•„ ๋ณด์ธ๋‹ค!"

"ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ Unix ์Šคํƒ€์ผ์—์„œ Windows ์Šคํƒ€์ผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ด…์‹œ๋‹ค. Unix๋Š” ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ /ํด๋”๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ณ  Windows๋Š” \๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์†”๋ฃจ์…˜ 1.char ์–ด๋ ˆ์ด ์‚ฌ์šฉ

์•”ํ˜ธ ๋…ธํŠธ
Scanner console = new Scanner(System.in);
String path = console.nextLine();

char[] chars = path.toCharArray();
for (int i = 0; i < chars.length; i++)
   if (chars[i] == '/')
      chars[i] = '\\';

String result = new String(chars);
System.out.println(result);
์Šค์บ๋„ˆ ๊ฐœ์ฒด ๋งŒ๋“ค๊ธฐ
์ฝ˜์†”์—์„œ ๋ผ์ธ ์ฝ๊ธฐ ๋ฌธ์ž์—ด์„

๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
๋ฌธ์ž๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค .
๋ฌธ์ž๊ฐ€ ์ด๋ฉด ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค . ํƒˆ์ถœํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๋ฌธ์ž ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค./
\


์†”๋ฃจ์…˜ 2.split() ๋ฐ ๋ฐฉ๋ฒ• ์‚ฌ์šฉ join():

์•”ํ˜ธ ๋…ธํŠธ
Scanner console = new Scanner(System.in);
String path = console.nextLine();

String array[] = path.split("\\/");


String result = String.join("\\", array);


System.out.println(result);
์Šค์บ๋„ˆ ๊ฐœ์ฒด ๋งŒ๋“ค๊ธฐ
์ฝ˜์†”์—์„œ ํ•œ ์ค„ ์ฝ๊ธฐ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด

๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค . ๋ฌธ์ž ๋Š” ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค(๋‘ ๊ฐœ์˜ ์ถ”๊ฐ€ ์Šฌ๋ž˜์‹œ๋Š” ์ด์ค‘ ์ด์Šค์ผ€์ดํ”„์˜ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค). strings ๋ฐฐ์—ด ์˜ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค . ๋Š” ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค(์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๋จ). ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค./
\

์†”๋ฃจ์…˜ 3. ๋ฐฉ๋ฒ• ์‚ฌ์šฉ replace(char oldChar, char newChar):

์•”ํ˜ธ ๋…ธํŠธ
Scanner console = new Scanner(System.in);
String path = console.nextLine();

String result = path.replace('/', '\\');

System.out.println(result);
์Šค์บ๋„ˆ ๊ฐœ์ฒด ๋งŒ๋“ค๊ธฐ
์ฝ˜์†”์—์„œ ํ•œ ์ค„ ์ฝ๊ธฐ ํ•œ ๋ฌธ์ž๋ฅผ

๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค
(๋‘ ๋ฒˆ์งธ ๋ฌธ์ž๋Š” ์ด์Šค์ผ€์ดํ”„๋จ)
. ๋ฌธ์ž์—ด์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

"์ €๋Š” ์„ธ ๋ฒˆ์งธ ์†”๋ฃจ์…˜์ด ๊ฐ€์žฅ ๋งˆ์Œ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์„ธ ๊ฐ€์ง€ ๋ชจ๋‘ ์—ฐ์Šตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

"์ž˜ํ–ˆ์–ด, ์•„๋ฏธ๊ณ . ์ด๋ฏธ ์ƒˆ๋กœ์šด ์ง€์‹์„ ์‹คํ–‰์— ์˜ฎ๊ธฐ๋Š” ๋ฐ ์กฐ๋ฐ”์‹ฌ์„ ๋‚ด๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๊ตฐ. ์ˆ˜์—…์€ ๋๋‚ฌ์–ด."

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION