
문자열의 일부 구간을 잘라서 새로운 문자열로 반환합니다.
String word = "hello";
word.substring(1); // "ello" (1번 인덱스부터 끝까지)
word.substring(1, 3); // "el" (1번부터 3번 전까지, 즉 1,2 인덱스)
substring(beginIndex): beginIndex부터 끝까지substring(beginIndex, endIndex): beginIndex 이상 ~ endIndex 미만(끝 인덱스는 포함 안 됨)JadenCase 문제에서 활용된 부분:
String lower = "hello";
char first = Character.toUpperCase(lower.charAt(0)); // 'H'
String rest = lower.substring(1); // "ello"
String result = first + rest; // "Hello"
→ charAt(0)으로 첫 글자만 빼서 대문자로 바꾸고, substring(1)으로 "첫 글자를 제외한 나머지"를 가져온 다음 합치는 패턴입니다. "첫 글자만 다르게 처리하고 싶을 때" 자주 쓰는 조합입니다.
| 메서드 | 역할 | 반환 타입 |
|---|---|---|
substring(i) | i번째부터 끝까지 자르기 | String |
substring(i, j) | i부터 j 미만까지 자르기 | String |