[자바 Utils&LIb] 45. JRE 라이브러리 (String)

김건우's avatar
Feb 12, 2025
[자바 Utils&LIb] 45. JRE 라이브러리 (String)

1. 문자열의 길이

String s1 = "abcd"; System.out.println("s1 length : " + s1.length());

2. 특정 index의 문자 확인하기

String s2 = "abcd"; System.out.println("s2 char : " + s1.charAt(2));

3. 문자열 비교

System.out.println("abcd".equals("abcd"));

4. 문자열 추출

String s3 = "abcd"; System.out.println(s3.substring(0, 2));

5. 문자열 검색

String s4 = "abcd"; System.out.println(s4.indexOf("c"));

6. 문자열 포함 여부

String s5 = "abcd"; System.out.println(s5.contains("k"));

7. 문자열 대소문자 변경

String s6 = "Abcd"; System.out.println(s6.toUpperCase()); System.out.println(s6.toLowerCase());

8. 문자열 치환

int age = 10; String s7 = "내 나이는 $age고, 난 내 나이 $age가 좋아".replace("$age", age + ""); System.out.println(s7);

9. 앞 뒤 공백 제거

String s8 = "abcd"; System.out.println(s8.trim());

10. 문자열 분리

String s9 = "a:b:c:d"; String[] r9 = s9.split(":"); System.out.println(r9[0]); System.out.println(r9[1]);
Share article

gunwoo