String

non-primitive

replaceAll(), replace()

public class Main
{
    public static void main(String[] args)
    {
        String str = "Hello, World_123!!";
 
        str = str.replaceAll("[^\\w+]", "");
        System.out.println(str);
    }
}
  • 결과 : HelloWorld_123

public class Main
{
    public static void main(String[] args)
    {
        String str = "Hello, World_123!!";
        String charsToRemove = ",_!";
 
        for (char c : charsToRemove.toCharArray()) {
            str = str.replace(String.valueOf(c), "");
        }
 
        System.out.println(str);
    }
}
  • 결과 : Hello World123

toCharArray()

public class Main
{
    public static void main(String[] args)
    {
        String str = "Soobin";

        char[] c = new char[str.length()];
        for (int i = 0; i < str.length(); i++)
        {
            c[i] = str.charAt(i);
            System.out.println(c[i]);
        }

        String str2 = "soobin";
        char[] arr = str2.toCharArray();
        for (char value : arr) {
            System.out.println(value);
        }
    }
}

indexOf(), lastIndexOf()

public class Main
{
    public static void main(String[] args)
    {
        String str = "Sooobin";

        int a = str.indexOf("o"); // 1
        int b = str.indexOf("o", 2); // 2
        int c = str.lastIndexOf("o"); // -1 (no o from index 3

        System.out.println(a + " : " +  str.charAt(a)); // 1o (index 1 is o
        System.out.println(b + " : " + str.charAt(b)); // 2o (index 2 is o
        System.out.println(c + " : " + str.charAt(c)); // 3o (index 3 is o
    }
}

1 : o

2 : o

3 : o

charAt()

str.charAt(idx);

toUpperCase(), toLowerCase()

public class Main
{
    public static void main(String[] args)
    {
        String str = "SoooBin";

        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }
}

SOOOBIN

sooobin

equals(str2)

str.equals(str2);

compareTo(str2)

str.compareTo(str2);
//returns s1-s2, in dictionary order so a-b returns -1

contains(str2)

str.contains(str2);

length()

str.length();

public class Main
{
    public static void main(String[] args)
    {
        String str = "SoooBin";
        String str2 = "SoooBin";
        String str3 = new String("SoooBin");
        String str4 = "SoooBin22";
        System.out.println(str == str2); // true
        System.out.println(str == str3); // false
        System.out.println(str.equals(str2)); // true
        System.out.println(str.equals(str3)); // true
        System.out.println(str.compareTo(str2)); // 0
        System.out.println(str.compareTo(str4)); // -2
        System.out.println(str.contains("oo")); // true

    }
}

startsWith("str2"), endWith("str2")

public class Main
{
    public static void main(String[] args)
    {
        String str = "SoooBin";
        System.out.println(str.startsWith("S")); // true
        System.out.println(str.startsWith("s")); // false
        System.out.println(str.endsWith("Bin")); // true
        System.out.println(str.endsWith("bin")); // false

    }
}

substring(incl,excl)

str.substring(incl,excl)
// incl: inclusive index, included, 
// excl: exclusive index, exclude

public class Main
{
    public static void main(String[] args)
    {
        String str = "SooBin Jung";
        // substring() 메소드를 사용하여 문자열을 추출
        String str1 = str.substring(0, 6);
        String str2 = str.substring(6);

        System.out.println(str1); // SooBin
        System.out.println(str2); //  Jung
    }
}

split("regrex")

str_array = str.split(“ “);
//return string array separated by spaces. 
// “Hello world” returns [“Hello”,”world”]
public class Main
{
    public static void main(String[] args)
    {
        String str = "SooBin Jung";
        // split
        String[] strArr = str.split(" ");
        for (String s : strArr)
        {
            System.out.println(s);
        }
    }
}

trim()

public class Main
{
    public static void main(String[] args)
    {
        String str = "  SooBin Jung  ";
        // trim
        System.out.println(str.trim()); // "SooBin Jung"
    }
}

Ref

StringBuilder

public class StringBuilder_Ex {
    public static void main(String[] args)  {
        String s = "abcdefg";
        StringBuilder sb = new StringBuilder(s); // String -> StringBuilder
		
        System.out.println("처음 상태 : " + sb); //처음상태 : abcdefg
        System.out.println("문자열 String 변환 : " + sb.toString()); //String 변환하기
        System.out.println("문자열 추출 : " + sb.substring(2,4)); //문자열 추출하기
        System.out.println("문자열 추가 : " + sb.insert(2,"추가")); //문자열 추가하기
        System.out.println("문자열 삭제 : " + sb.delete(2,4)); //문자열 삭제하기
        System.out.println("문자열 연결 : " + sb.append("hijk")); //문자열 붙이기
        System.out.println("문자열의 길이 : " + sb.length()); //문자열의 길이구하기
        System.out.println("용량의 크기 : " + sb.capacity()); //용량의 크기 구하기
        System.out.println("문자열 역순 변경 : " + sb.reverse()); //문자열 뒤집기
        System.out.println("마지막 상태 : " + sb); //마지막상태 : kjihgfedcba
    }
}

StringBuffer

public class StringBuffer_Ex {
    public static void main(String[] args)  {
        String s = "abcdefg";
        StringBuffer sb = new StringBuffer(s); // String -> StringBuffer
		
        System.out.println("처음 상태 : " + sb); //처음상태 : abcdefg
        System.out.println("문자열 String 변환 : " + sb.toString()); //String 변환하기
        System.out.println("문자열 추출 : " + sb.substring(2,4)); //문자열 추출하기
        System.out.println("문자열 추가 : " + sb.insert(2,"추가")); //문자열 추가하기
        System.out.println("문자열 삭제 : " + sb.delete(2,4)); //문자열 삭제하기
        System.out.println("문자열 연결 : " + sb.append("hijk")); //문자열 붙이기
        System.out.println("문자열의 길이 : " + sb.length()); //문자열의 길이구하기
        System.out.println("용량의 크기 : " + sb.capacity()); //용량의 크기 구하기
        System.out.println("문자열 역순 변경 : " + sb.reverse()); //문자열 뒤집기
        System.out.println("마지막 상태 : " + sb); //마지막상태 : kjihgfedcba
    }
}

Last updated