jennet1 님의 블로그

문자열 다루기 본문

WEB/JAVA

문자열 다루기

jennet1 2025. 1. 3. 12:25

1. 문자열 길이 구하기

package Pool;

public class ch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 문자열 길이 
		String str = "Hello world";
		System.out.println(str.length());
		
	}

}

 

출력

11

 

2. 문자열 비교 (같은지 다른지)

  • equals() : 두 문자열이 정확히 동일한지 비교
  • equalsIgnoreCase() : 대소문자 구분 없이 비교
package Pool;

public class ch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str1 = "Hello";
		String str2 = "hello";
		
		System.out.println(str1.equals(str2)); // 두 문자가 정확히 동일한지 
		System.out.println(str1.equalsIgnoreCase(str2)); // 대소문자 없이 비교
		
	}

}

출력

false
true

 

3. 부분 문자열 추출 (Substring)

package Pool;

public class ch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 문자열 추출하기
		String str = "Hello World";
		String subStr = str.substring(7); // 7번째 부터 끝까지
		System.out.println(subStr);
		
		String subStr2 = str.substring(0,5); // 0 부터 5까지
		System.out.println(subStr2);	
	}

}

출력

orld
Hello

 

4. 문자열 검색 (찾기)

package Pool;

public class ch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "Hello World";
		
        System.out.println(str.indexOf("o"));      // 4 (첫 번째 o의 위치)
        System.out.println(str.lastIndexOf("o"));  // 8 (마지막 o의 위치)
        System.out.println(str.contains("World")); // true (포함됨)
        System.out.println(str.contains("level"));   // false
	}

}

 

출력

4
7
true
false

 

5. 모든 문자열 대소문자로 변환

package Pool;

public class ch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "Hello World";
		
        System.out.println(str.toLowerCase());  // hello, world!
        System.out.println(str.toUpperCase());  // HELLO, WORLD!
	}

}

출력

hello world
HELLO WORLD

6. 문자열 빈값 확인

package pratic01;

public class Main5 {
	
	public static void main(String[] args) {
		String str = "";
		System.out.println(str.isEmpty());
	}

}

출력

true

6. 특정 인덱스 문자 반환

package pratic01;

public class Main5 {
	
	public static void main(String[] args) {
		String str = "Hello";
		System.out.println(str.charAt(2));
	}

}

출력

l

 

7. 문자열 바꾸기

package pratic01;

public class Main5 {
	
	public static void main(String[] args) {
		String str = "Hello, World!";
		System.out.println(str.replace('o', 'a'));  // 출력: "Hella, Warld!"
		System.out.println(str.replace("World", "Java")); // 출력: "Hello, Java!"
	}

}

출력

Hella, Warld!
Hello, Java!

7. 문자열 분리

package pratic01;

public class Main5 {
	
	public static void main(String[] args) {
		String str = "Apple,Banana,Cherry";
		String[] fruits = str.split(",");
		for (String fruit : fruits) {
		    System.out.println(fruit);
		}
	}

}

 

출력

Apple
Banana
Cherry

8. 문자열 결합

package pratic01;

public class Main5 {

	public static void main(String[] args) {
		String[] fruits = { "Apple", "Banana", "Cherry" };
		String result = String.join(", ", fruits);
		System.out.println(result); // 출력: "Apple, Banana, Cherry"
	}

}

출력

Apple, Banana, Cherry

 

8. 문자열 배열로 변환

package pratic01;

public class Main5 {

	public static void main(String[] args) {
		String str = "Hello";
		char[] chars = str.toCharArray();
		for (char c : chars) {
		    System.out.print(c + " "); // 출력: H e l l o
		}
	}

}

출력

H e l l o