๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Java

[Java] String ํด๋ž˜์Šค

by dkswnkk 2022. 4. 5.

์„œ๋ก 

c์–ธ์–ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด์„ charํ˜•์˜ ๋ฐฐ์—ด๋กœ ๋‹ค๋ฃจ์—ˆ์ง€๋งŒ ์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์ž์—ด์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ String ํด๋ž˜์Šค์ธ๋ฐ, String ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๋‹ค๋ฃจ๋Š”๋ฐ ํ•„์š”ํ•œ ์ˆ˜๋งŽ์€ ๋ฉ”์„œ๋“œ๋“ค์„ ํ•จ๊ป˜ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” ์ด๋Ÿฌํ•œ String ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋“ค์— ๋Œ€ํ•ด ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ชฉ์ฐจ

1. ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ(immutable) ํด๋ž˜์Šค
2. ๋ฌธ์ž์—ด์˜ ๋น„๊ต(๋ฆฌํ„ฐ๋Ÿด vs ์ธ์Šคํ„ด์Šค, == vs equals)
3. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด
4. String ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ 

 

1) ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ(immutable) ํด๋ž˜์Šค

ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ String ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์€ ์ฝ์–ด ์˜ฌ ์ˆ˜๋งŒ ์žˆ๊ณ , ๋ณ€๊ฒฝํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด '+'์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ ์ธ์Šคํ„ด์Šค ๋‚ด์˜ ๋ฌธ์ž์—ด์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด("ab")์ด ๋‹ด๊ธด String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    public static void main(String[] args) {
        String a = "a";
        String b = "b";
        a = a + b;
    }

์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

์ด์ฒ˜๋Ÿผ ๋ง์…ˆ ์—ฐ์‚ฐ์ž '+'๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ์€ ๋งค ์—ฐ์‚ฐ ์‹œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๊ฐ€์ง„ String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ๊ฐ€๋Šฅํ•œ ๊ฒฐํ•ฉ ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด๊ฐ„์˜ ๊ฒฐํ•ฉ์ด๋‚˜ ์ถ”์ถœ ๋“ฑ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์ž‘์—…์ด ๋งŽ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” String ํด๋ž˜์Šค ๋Œ€์‹  StringBuffer ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. StringBuffer ์ธ์Šคํ„ด์Šค์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์€ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ•˜๋‚˜์˜ StringBuffer ์ธ์Šคํ„ด์Šค๋งŒ์œผ๋กœ๋„ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

2-1) ๋ฌธ์ž์—ด์˜ ๋น„๊ต(๋ฆฌํ„ฐ๋Ÿด vs ์ธ์Šคํ„ด์Šค)

๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ๋•Œ์—๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• (String s = "abc");
  2. String ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•(String s = new String("abc"));
    public static void main(String[] args) {
        String str1 = "abc";    // ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด "abc"์˜ ์ฃผ์†Œ๊ฐ€ str1์— ์ €์žฅ๋จ
        String str2 = "abc";    // ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด "abc"์˜ ์ฃผ์†Œ๊ฐ€ str2์— ์ €์žฅ๋จ
        String str3 = new String("abc");    // ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
        String str4 = new String("abc");    // ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
    }

String ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ๊ฒฝ์šฐ์—๋Š” new ์—ฐ์‚ฐ์ž์— ์˜ํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์ด ์ด๋ฃจ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ํด๋ž˜์Šค๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ๋  ๋•Œ ์ž๋™์ ์œผ๋กœ ๋ฏธ๋ฆฌ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.)

๋”ฐ๋ผ์„œ ์œ„ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ ์ƒํ™ฉ์„ ์ด๋ฏธ์ง€๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ขŒ) ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ์ง€์ •, ์šฐ) Stringํด๋ž˜์Šค ์ƒ์„ฑ์ž

 

2-2) ๋ฌธ์ž์—ด์˜ ๋น„๊ต('==' vs 'equals')

์ž๋ฐ”์—์„œ == ์—ฐ์‚ฐ์€ ์ฃผ์†Œ ๊ฐ’์ด ๊ฐ™์€์ง€ ์•„๋‹Œ์ง€ ๋น„๊ตํ•˜์ง€๋งŒ, equals() ์—ฐ์‚ฐ์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์ฃผ์†Œ ๊ฐ’์„ ๋น„๊ตํ•˜์ง€๋งŒ String ํด๋ž˜์Šค์—์„œ equals()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜์—ฌ ์ฃผ์†Œ ๊ฐ’์ด ์•„๋‹Œ ๊ฐ’์„ ๋น„๊ตํ•˜๋„๋ก ์žฌ์ •์˜ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค.

    public static void main(String[] args) {
    
        String str1 = "abc";    // ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด "abc"์˜ ์ฃผ์†Œ๊ฐ€ str1์— ์ €์žฅ๋จ
        String str2 = "abc";    // ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด "abc"์˜ ์ฃผ์†Œ๊ฐ€ str2์— ์ €์žฅ๋จ
        String str3 = new String("abc");    // ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
        String str4 = new String("abc");    // ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
        
        if (str1 == str2) System.out.println("true");
        else System.out.println("false");   
        if (str3 == str4) System.out.println("true");
        else System.out.println("false");
        
        System.out.println("str1.equals(str2)= " + str1.equals(str2));      
        System.out.println("str3.equals(str4)= " + str3.equals(str4));
    }

์‹คํ–‰ ๊ฒฐ๊ณผ

equals()๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ๋‘ ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ("abc")์„ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „๋ถ€ true๋ฅผ ๊ฒฐ๊ณผ๋กœ ์–ป์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ๊ฐ String ์ธ์Šคํ„ด์Šค์˜ ์ฃผ์†Œ๋ฅผ ๋“ฑ๊ฐ€๋น„๊ต์—ฐ์‚ฐ์ž '=='๋กœ ๋น„๊ตํ–ˆ์„ ๋•Œ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

3) ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด

์ž๋ฐ” ์†Œ์ŠคํŒŒ์ผ์— ํฌํ•จ๋œ ๋ชจ๋“  ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ์ปดํŒŒ์ผ ์‹œ์— ํด๋ž˜์Šค ํŒŒ์ผ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ ๊ฐ™์€ ๋‚ด์šฉ์˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ํ•œ ๋ฒˆ๋งŒ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋„ String ์ธ์Šคํ„ด์Šค์ด๊ณ , ํ•œ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ณต์œ ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

public class Test {
    public static void main(String[] args) {
        String s1 = "AAA";
        String s2 = "AAA";
        String s3 = "AAA";
        String s4 = "AAA";
    }
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด "AAA"๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” String ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜ ์ƒ์„ฑ๋œ ํ›„, ์ฐธ์กฐ๋ณ€์ˆ˜ s1, s2, s3๋Š” ๋ชจ๋‘ ์ด ๊ฐ™์€ String ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ํŒŒ์ผ์—๋Š” ์†Œ์ŠคํŒŒ์ผ์— ํฌํ•จ๋œ ๋ชจ๋“  ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ชฉ๋ก์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค ํŒŒ์ผ์ด ํด๋ž˜์Šค ๋กœ๋”์— ์˜ํ•ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ๋•Œ, ์ด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฆฌํ„ฐ๋Ÿด๋“ค์ด JVM๋‚ด์— ์žˆ๋Š” '์ƒ์ˆ˜ ์ €์žฅ์†Œ(constant pool)'์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ์ด๊ณณ์— "AAA"์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜์–ด ์ €์žฅ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํด๋ž˜์Šค ๋กœ๋”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€๋Š” ๊ณผ์ •๊ณผ JVM์˜ ๊ตฌ์„ฑ์š”์†Œ ๊ฐ™์€ ๊ฒฝ์šฐ ์ œ๊ฐ€ ์ด์ „์— ์ •๋ฆฌํ•œ ๊ฒŒ์‹œ๊ธ€์ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹ค ๋•Œ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

[Java] JVM์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์„ฑ์š”์†Œ์™€ ์˜์—ญ์— ๊ด€ํ•œ ๊ณ ์ฐฐ

์„œ๋ก  ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋ฉด, JVM์€ ์‹œ์Šคํ…œ์œผ๋กœ๋ถ€ํ„ฐ ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›๊ณ  JVM์€ ์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์šฉ๋„์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ์˜์—ญ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์—ฌ

dkswnkk.tistory.com

 

4) String ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ

1. String(String s)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(s)์„ ๊ฐ–๋Š” String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
 */
 
String s = new String ("Hello");
System.out.println(s);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Hello

 

2. String[char[] value)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(value)์„ ๊ฐ–๋Š” String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
 */
 
char[] c = {'H', 'e', 'l', 'l', 'o'};
String s = new String(c);
System.out.println(s);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Hello

 

3. String(StringBuffer buf)

 /*
    StringBuffe ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์˜ String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
 */
 
StringBuffer sb = new StringBuffer("Hello");
String s = new String(sb);
System.out.println(sb);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Hello

 

4. charAt(index)

 /*
    ์ง€์ •๋œ ์œ„์น˜(index)์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ์•Œ๋ ค์ค€๋‹ค.(index๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘)
 */
 
String s = "Hello";
System.out.println(s.charAt(1));

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: e

 

4. int compareTo(String str)

 /*
    ๋ฌธ์ž์—ด(str)๊ณผ ์‚ฌ์ „์ˆœ์„œ๋กœ ๋น„๊ตํ•œ๋‹ค.
    ๊ฐ™์œผ๋ฉด 0, ์‚ฌ์ „์ˆœ์œผ๋กœ ์ด์ „์ด๋ฉด ์Œ์ˆ˜, ์ดํ›„๋ฉด ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
int i = "aaa".compareTo("aaa");
int i2 = "aaa".compareTo("bbb");
int i3 = "bbb".compareTo("aaa");

System.out.println(i);
System.out.println(i2);
System.out.println(i3);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 0, 1, 3

 

5. String concat(String str)

 /*
    ๋ฌธ์ž์—ด(str)์„ ๋’ค์— ๋ง๋ถ™์ธ๋‹ค.
 */
 
String s = "Hello";
String s2 = s.concat(" World");
System.out.println(s2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Hello World

 

6. boolean endsWith(String suffix)

 /*
    ์ง€์ •๋œ ๋ฌธ์ž์—ด(suffix)๋กœ ๋๋‚˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
 */
 
String s = "Hello.txt";
boolean b = s.endsWith("txt");
System.out.println(b);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: true

 

7. boolean contains(CharSequence s)

 /*
    ์ง€์ •๋œ ๋ฌธ์ž์—ด(s)์ด ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
 */
 
String s = "abcedfg";
boolean b = s.contains("bc");
System.out.println(b);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: true

 

8. boolean equalsIgnoreCase(String str)

 /*
    ๋ฌธ์ž์—ด๊ณผ String ์ธ์Šคํ„ด์Šค์˜ ๋ฌธ์ž์—ด์„ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์—†์ด ๋น„๊ตํ•œ๋‹ค.
 */
 
String s = "Hello";
boolean b = s.equalsIgnoreCase("hello");
System.out.println(b);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: true

 

9. int indexOf(int ch)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž(ch)๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ค€๋‹ค. 
    ๋ชป ์ฐพ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(index๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘)
 */
 
String s = "Hello";
int idx1 = s.indexOf('o');
int idx2 = s.indexOf('k');

System.out.println(idx1);
System.out.println(idx2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 4, -1

 

10. int indexOf(int ch, int pos)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž(ch)๊ฐ€ ๋ฌธ์ž์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ์ง€์ •๋œ ์œ„์น˜(pos)๋ถ€ํ„ฐ ํ™•์ธํ•˜์—ฌ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
    ๋ชป ์ฐพ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(index ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘)
 */
 
String s = "Hello";
int idx1 = s.indexOf('e', 0);
int idx2 = s.indexOf('e' ,2);

System.out.println(idx1);
System.out.println(idx2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 1, -1

 

11. int indexOf(String str)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ทธ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
    ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(index๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘)
 */
 
String s = "ABCDEFG";
int idx = s.indexOf("CD");

System.out.println(idx);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 2

 

12. String intern()

 /*
    ๋ฌธ์ž์—ด์„ ์ƒ์ˆ˜ํ’€(constant pool)์— ๋“ฑ๋กํ•œ๋‹ค.
    ์ด๋ฏธ ์ƒ์ˆ˜ํ’€์— ๊ฐ™์€ ๋‚ด์šฉ์˜ ๋ฌธ์ž์—ด์ด ์žˆ์„ ๊ฒฝ์šฐ ๊ทธ ๋ฌธ์ž์—ด์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String s = new String("abc");
String s2 = new String("abc");
boolean b = (s == s2);
boolean b2 = s.equals(s2);
boolean b3 = (s.intern() == s2.intern());

System.out.println(b);
System.out.println(b2);
System.out.println(b3);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: false, true, true

 

13. int lastIndexOf(int ch)

 /*
    ์ง€์ •๋œ ๋ฌธ์ž ๋˜๋Š” ๋ฌธ์ž์ฝ”๋“œ๋ฅผ ๋ฌธ์ž์—ด์˜ ์˜ค๋ฅธ์ชฝ ๋์—์„œ๋ถ€ํ„ฐ ์ฐพ์•„์„œ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
    ๋ชป ์ฐพ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String s = "java.lang.Object";
int idx1 = s.lastIndexOf('.');
int idx2 = s.indexOf('.');

System.out.println(idx1);
System.out.println(idx2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 9, 4

 

14. int lastIndexOf(String str)

 /*
    ์ง€์ •๋œ ๋ฌธ์ž์—ด์„ ์ธ์Šคํ„ด์Šค์˜ ๋ฌธ์ž์—ด ๋์—์„œ๋ถ€ํ„ฐ ์ฐพ์•„์„œ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ค€๋‹ค.
    ๋ชป ์ฐพ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String s = "java.lang.java";
int idx1 = s.lastIndexOf("java");
int idx2 = s.indexOf("java");

System.out.println(idx1);
System.out.println(idx2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: 10, 0

 

15. String trim()

 /*
    ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ ๋๊ณผ ์˜ค๋ฅธ์ชฝ ๋์— ์žˆ๋Š” ๊ณต๋ฐฑ์„ ์—†์•ค ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    ์ด ๋•Œ ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ์žˆ๋Š” ๊ณต๋ฐฑ์€ ์ œ๊ฑฐ๋˜์ง€ ์•Š๋Š”๋‹ค.
 */
 
String s = "     Hello World    ";
String s1 = s.trim();

System.out.println(s1);
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Hello World

 

16. boolean startsWith(String prefix)

 /*
    ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด(prefix)๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.
 */
 
String s = "java.lang.Object";
boolean b = s.startsWith("java");
boolean b2 = s.startsWith("lang");

System.out.println(b);
System.out.println(b2);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: true, false

 

17. String replace(char old, char new)

 /*
    ๋ฌธ์ž์—ด ์ค‘์˜ ๋ฌธ์ž(old)๋ฅผ ์ƒˆ๋กœ์šด ๋ฌธ์ž(new)๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String s = "Hello";
String s1 = s.replace('H', 'h');

System.out.println(s1);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: hello

 

18. String replace(CharSequence old, CharSequence new)

 /*
    ๋ฌธ์ž์—ด ์ค‘์˜ ๋ฌธ์ž์—ด(old)์„ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด(new)๋กœ ๋ชจ๋‘ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String s = "Hello";
String s1 = s.replace("ll", "LL");

System.out.println(s1);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: HeLLo

 

19. String replaceAll(String regex, String replacement)

 /*
    ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง€์ •๋œ ๋ฌธ์ž์—ด(regex)๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์„ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด(replacement)๋กœ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•œ๋‹ค.
 */
 
String ab = "AABBAABB";
String r = ab.replaceAll("BB", "bb");
        
System.out.println(r);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: AAbbAAbb

 

20. String replaceFirst(String regex, String replacement)

 /*
    ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง€์ •๋œ ๋ฌธ์ž์—ด(regex)๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ ์ค‘, ์ฒซ ๋ฒˆ์งธ ๊ฒƒ๋งŒ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด(replacement)๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
 */
 
String ab = "AABBAABB";
String r = ab.replaceAll("BB", "bb");
        
System.out.println(r);

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: AAbbAABB

 

21. Srtring [] split(String regex)

 /*
    ๋ฌธ์ž์—ด์„ ์ง€์ •๋œ ๋ถ„๋ฆฌ์ž(regex)๋กœ ๋‚˜๋ˆ„์–ด ๋ฌธ์ž์—ด ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 */
 
String animals = "dog,cat,bear";
String [] arr  = animals.split(",");
        
// arr[0] = dog, arr[1] = cat, arr[2] = bear

๋Œ“๊ธ€