프로그래밍 언어/JAVA
기본 API - 문자열 클래스
jjonse
2023. 1. 26. 16:49
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
}
}