[Java] 문자열

es.Seong·2023년 7월 31일
0

Java

목록 보기
1/2

본 게시물은 "점프 투 자바"를 학습 & 정리한 내용 입니다.
https://wikidocs.net/205

1. 문자열 내장 메소드

1. equals

두 개의 문자열이 동일한지 비교 후 결과값 반환

public class CH02_String {
    public static void main(String[] args) {
        String a= "Java";
        String b = "python";
        String c = "python";
        System.out.println(a.equals(b));
        System.out.println(b.equals(c));
    }
}

결과
false
true

2.indexOf

문자열에서 특정 문자열이 시작되는 인덱스 값 반환

public class Ch02_indexOf {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.indexOf("World"));
    }
}

인덱스 값은 0부터 시작
world에서 w의 인덱스 값인 6 리턴

결과
6

3. contains

특정 문자열의 포함여부 반환

public class Ch02_contains {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.contains("World"));
    }
}

결과
true

4. charAt

문자열에서 특정 위치의 문자(char)값 반환

public class Ch02_charAt {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.charAt(0));
    }
}

결과
H

문자열에서 인덱스 0에 위치한 H 반환

5. replaceAll

문자열을 교체할 때 사용

public class Ch02_replaceAll {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.replaceAll("World","Hi"));
    }
}

결과
Hello Hi

Hello World 문자열에서 World 문자열 값을 Hi로 교체된다.

6. substring

문자열의 특정부분을 뽑아낼 때 사용

public class Ch02_substring {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.substring(0,4));
    }
}

결과
Hell

메소드 내 파라미터 -> (시작 인덱스, 종료 인덱스 +1)이다.
예제처럼 (0,4) 일 경우 0, 1, 2, 3 부분을 출력한다.

7. toUpperCase

문자열을 모두 대문자로 변경한다.
(소문자의 경우 toLowerCase 사용)

public class ch02_toUpperCase {
    public static void main(String[] args){
        String a ="Hello World";
        System.out.println(a.toUpperCase());
    }
}

결과
HELLO WORLD

8. split

문자열을 특정 문자열을 기준으로 나누어 배열로 반환하는 메소드.

import java.util.Arrays;

public class ch02_split {
    public static void main(String[] args){
        String a = "a:b:c:d";
        String[] result = a.split(":");  
        System.out.println(Arrays.toString(result)); // 배열을 문자열로 변환 후 출력해야함. 
    }
}

결과
[a, b, c, d]

2. 문자열 포맷팅

public class ch02_formatting {
    public static void main(String[] args){
        // 숫자 바로 대입
        System.out.println(String.format("I have %d dollars", 10)); 

        // 문자열 바로 대입
        System.out.println(String.format("I have %s dollars","ten"));

        // 선언한 변수 대입
        int d =10;
        System.out.println(String.format("I have %d dollars",d));

        //2개 이상 값 넣기
        String dd ="ten";
        System.out.println(String.format("I have %d dollars, I have %s dollars",d,dd));

        //println -> printf로 교체
        System.out.printf("I have %d dollars, I have %s dollars",d,dd);

        /* String.format 과 System.out.printf의 차이는 전자는 문자열을 리턴하는 메서드이고 후자는 문자열을 출력하는 메서드 */
    }
}
profile
Graduate student at Pusan National University, majoring in Artificial Intelligence

0개의 댓글