1. 생성
String s1 = "hello";
String s2 = new String("hello"); // 잘 안 씀
2. 길이, 문자 접근
s1.length(); // 길이: 5
s1.charAt(1); // 'e'
3. 비교
s1.equals("hello"); // true (내용 비교)
s1.equalsIgnoreCase("HELLO"); // true (대소문자 무시)
s1.compareTo("abc"); // 사전순 비교 (같으면 0, 앞이면 음수, 뒤면 양수)
s1.startsWith("he"); // true
s1.endsWith("lo"); // true
4. 검색
s1.indexOf("l"); // 2 (처음 찾은 위치)
s1.lastIndexOf("l"); // 3 (마지막 위치)
s1.contains("ell"); // true
5. 추출 / 잘라내기
s1.substring(1); // "ello"
s1.substring(1, 4); // "ell" (1 <= idx < 4)
6. 변환
s1.toUpperCase(); // "HELLO"
s1.toLowerCase(); // "hello"
s1.trim(); // 앞뒤 공백 제거
String.valueOf(123); // "123"
Integer.parseInt("123");// 123 (문자열 → 숫자)
7. 교체
s1.replace("l", "x"); // "hexxo" (모두 교체)
s1.replaceFirst("l", "x"); // "hexlo" (첫 번째만 교체)
s1.replaceAll("[aeiou]", "*"); // 정규식 기반: "h*ll*"
8. 분할 & 합치기
String[] parts = "a,b,c".split(","); // {"a","b","c"}
String joined = String.join("-", parts); // "a-b-c"
9. 포맷팅
String f = String.format("이름: %s, 나이: %d", "봉규", 25);
// "이름: 봉규, 나이: 25"
10. 반복 / 문자열 빌더
"abc".repeat(3); // "abcabcabc" (Java 11+)
11. StringBuilder
StringBuilder sb = new StringBuilder();
sb.append("a");
sb.append("b");
sb.toString(); // "ab"
sb.insert(1, "x"); // "axb"
sb.deleteCharAt(0); // "xb"
sb.reverse(); // "bx"