기본 API - 문자열 클래스

2023. 1. 26. 16:49프로그래밍 언어/JAVA

charAt(), length() ,replace()

- 문자열의 한 글자만 가져오는 메소드다.

 

public class Test {
	public static void main(String[] args) {
    	String str = "abcd";
        //charAt() 메소드
        char x = str.charAt(0);
        System.out.println(x);
        
        
        //concat() 메소드
        String result = str.concat("efg");
        
        System.out.println(result);		//abcdefg
        
        
        //length() 메소드
        int x = str.length();
        System.out.println(x);		//4
        
        
        //replace() 메소드
        String answer = str.replace('a', 'x');
        System.out.println(answer);	//xbcd
        
        
        //substring 메소드
        String answer2 = str.substring(2);	//abcd라는 글자에서 2번 인덱스부터 마지막 까지를 잘라온다
        System.out.println(answer2);		//cd
        
        
        
    
    }
}

 

'프로그래밍 언어 > JAVA' 카테고리의 다른 글

String vs StringBuffer vs StringBuilder  (0) 2023.01.26
Wrapper 클래스  (0) 2023.01.26
FileOutputStream / PrintWriter  (0) 2023.01.26
Catch/ throw  (0) 2023.01.25
파일 입출력 - BufferedReader, BufferedWriter  (0) 2023.01.20