๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜•Java

[Java ์ดํ•ดํ•˜๊ธฐ] 9. ์ž๋ฐ”์˜ String(๋ฌธ์ž์—ด) ๊ธฐ์ดˆ (2)

by Luvbit 2024. 5. 12.


 

 

๐Ÿค String ํ•จ์ˆ˜ ์ข…๋ฅ˜ 

Java์˜ String ํด๋ž˜์Šค๋Š” ๋‹ค์–‘ํ•œ ๋ฌธ์ž์—ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๋Œ€ํ‘œ์ ์ธ String ๋ฉ”์„œ๋“œ๋“ค์— ๋Œ€ํ•ด ์˜ˆ์‹œ์™€ ํ•จ๊ป˜ ๋ถ€๊ฐ€ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜ ์„ค๋ช…
charAt(int index) ๋ฌธ์ž์—ด์—์„œ ์ง€์ •๋œ ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
substring(int beginIndex) ๋ฌธ์ž์—ด์˜ ์ง€์ •๋œ ์‹œ์ž‘ ์œ„์น˜๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
substring(int beginIndex, int endIndex) ๋ฌธ์ž์—ด์˜ ์ง€์ •๋œ ์‹œ์ž‘ ์œ„์น˜์—์„œ ๋ ์œ„์น˜ ์‚ฌ์ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
indexOf(String str) ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์‹œ์ž‘๋˜๋Š” ์ฒซ ์œ„์น˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
toLowerCase() ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
toUpperCase() ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์†Œ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
trim() ๋ฌธ์ž์—ด ์–‘์ชฝ์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
replace(char oldChar, char newChar) ๋ฌธ์ž์—ด์—์„œ ์ง€์ •๋œ ๋ชจ๋“  ์˜› ๋ฌธ์ž๋ฅผ ์ƒˆ ๋ฌธ์ž๋กœ ๋Œ€์ฒดํ•œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
split(String regex) ๋ฌธ์ž์—ด์„ ์ง€์ •๋œ ์ •๊ทœ ํ‘œํ˜„์‹์— ๋”ฐ๋ผ ๋ถ„ํ• ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
equals(Object anObject) ์ง€์ •๋œ ๊ฐ์ฒด์™€ ๋ฌธ์ž์—ด์ด ๋™๋“ฑํ•œ์ง€ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค. ๋™๋“ฑํ•˜๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
length() ๋ฌธ์ž์—ด์˜ ๊ธธ์ด(๋ฌธ์ž ์ˆ˜)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

 


 

 

1. CharAt()

String str = "Rabbit";
char result = str.charAt(0); // 'R'

//๋ฌธ์ž์˜ 0๋ฒˆ์จฐ ์ธ๋ฑ์Šค๋ฅผ ๊บผ๋‚ด๋Š” ๊ฒƒ.
//java์—์„œ๋Š” ์ˆœ์„œ๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

2.subString()

String str = "Rabbit";
String result = str.substring(2); // "abit"

//2๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค.

String str = "Rabbit";
String result = str.substring(1, 3); // "ab"

//1๋ฒˆ์งธ ๋ถ€ํ„ฐ 3๋ฒˆ์งธ ์ „๊นŒ์ง€ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค.

 

3.indexOf()

String str = "Rabbit";
int result = str.indexOf("bit"); // 3

//'bit'์ด ๋ช‡๋ฒˆ์งธ์—์„œ ์‹œ์ž‘ํ•˜๋Š”์ง€ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

4. toLowerCase()

String str = "Rabbit";
String result = str.toLowerCase(); // "rabbit"

//๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

 

5. toUpperCase()

String str = "Rabbit";
String result = str.toUpperCase(); // "RABBIT"

//๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

 

6. trim()

String str = "  Rabbit  ";
String result = str.trim(); // "Rabbit"

//๋ฌธ์ž์—ด ์–‘์ชฝ์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

 

7. replace()

String str = "Rabbit";
String result = str.replace('R', 'a'); // "aabbit"

//์ฒซ๋ฒˆ์งธ ์ธ์ž๊ฐ’์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ๋‘๋ฒˆ์งธ ์ธ์ž๊ฐ’์˜ ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

 

8. split()

String str = "It,Rabbit,Luvbit!";
String[] results = str.split(","); //["It", "Rabbit", "Luvbit!"]

','๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ชผ๊ฐœ์–ด ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค.

 

9.eqauls()

String str = "Rabbit";
boolean result = str.equals("Rabbit"); // true

 

10.length()

String str = "Rabbit";
int result = str.length(); //6

//length๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์€ ๊ต‰์žฅํžˆ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ์›น ํŽ˜์ด์ง€๋‚˜ ๊ฒŒ์ž„, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด
์ •๋ณด๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”!
๐Ÿฉต๐Ÿฐ๐Ÿฉต