String 클래스의 주요 메소드
String replacedStr = str.replace('l', 'p'); // "Heplo Worpd"
String replacedAllStr = str.replaceAll("o", "0"); // "Hell0 W0rld"
String caseStr = "hello world";
boolean isEqualIgnoreCase = str.equalsIgnoreCase(caseStr); // true
String anotherStr = "Hello World";
boolean isEqual = str.equals(anotherStr); // true
1. length() : int
문자열의 길이를 구한다.
String str = "Hello World";
int length = str.length(); // 11
2. charAt(int index) : Char
특정 인덱스의 문자열을 가져온다.
예를 들어 str은 Hello World이므로, 첫 번째 값은 H이다.
char ch = str.charAt(0) // 'H'
3. substring(int beginIndex, int endIndex) : String
문자열의 부분을 추출한다.
주의사항 1. substring(int beginIndex, int endIndex)의 endIndex는 포함되지 않는다는 것이다.
예를 들어, "Hello World"라는 단어가 있을 때, str.substring(6, 11)을 하면 "World"가 된다.
주의사항 2. substring(int beginIndex)의 경우 자동으로 endIndex는 마지막 글자가 된다.
예를 들어, "Hello World"라는 단어가 있을 때, str.substring(0)을 하면 "Hello World"가 된다.
String substr = str.substring(6, 11); // "World"
4. equals(Object anotherObject) : boolean
두 문자열의 내용이 동일한지 비교한다.
예를 들어서 str은 Hello World이고, anotherStr은 Hello World이기 때문에, 둘은 동일하다. 즉, true이다.
String anotherStr = "Hello World";
boolean isEqual = str.equals(anotherStr); // true
5. equalsIgnoreCase(String anotherString) : boolean
두 문자열을 대소문자 구분 없이 비교한다.
예를 들어서 str은 Hello World이고, anotherStr은 Hello World이기 때문에, 둘은 동일하다. 즉 true이다.
equals와 equalsIgnoreCase의 다른 점은, equals는 대소문자까지 구분하고, equalsIgnoreCase는 대소문자 구분 없이 비교한다.
String caseStr = "hello world";
boolean isEqualIgnoreCase = str.equalsIgnoreCase(caseStr); // true
6. startsWith(String prefix) / endsWith(String suffix) : boolean
-> 첫 번째, 마지막 "문자"를 반환하는 것 X , prefix나 suffix에 들어가는 단어로 시작/끝인지 판단
=> boolean 타입인 것 유의하기
문자열이 특정 문자열로 시작하거나 끝나는지 확인한다
예를 들어, str은 Hello World이기 때문에, Hello로 시작하고, World로 끝난다.
Hello로 시작하는 것이 맞기 때문에 startWith("Hello") 값은 True이고,
대소문자를 구분하기 때문에 world가 아니라 World로 끝나니, false이다.
boolean startsWithHello = str.startWith("Hello"); // true
boolean endsWithWorld = str.endsWith("world"); // false (대소문자 구분)
7. contains(CharSequence s) : boolean
문자열이 특정 문자열을 포함하는지 확인한다.
예를 들어, str은 Hello World이기 때문에 World를 포함한다. 즉, 값은 True이다.
boolean containsWorld = str.contains("World"); //true
8. replace(char oldChar, char newChar) / replaceAll(String regex, String replacement) : boolean
문자열에서 문자 또는 문자열을 다른 것으로 대체한다.
첫 번째로 replace는 해당 문자 중 가장 앞쪽에 있는 것만 대체문자로 대체한다.
예를 들어 str이 Hello World이고, str.replace('l', 'p')를 실행하면, l이 있던 자리에 p가 들어간다.
따라서 Heplo Worpd가 된다.
두 번째로 replaceAll은 같은 문자 모두를 대체한다.
예를 들어 str이 Hello World이고, str.replaceAll("o", "0")을 실행하면, o가 있던 자리 모두에 0이 들어간다.
따라서 Hell0 W0rld가 된다.
boolean containsWorld = str.contains("World"); //true
9. split(String regex) : String[]
문자열을 구분자로 분할한다.
예를 들어, str은 Hello World이므로, 구분자인 공백으로 분리하면 Hello와 World가 된다.
구분자로 분할하여 저장하기 때문에 문자열 배열과 함께 사용하는 것이 편리하다.
String[] parts = str.split(" "); // ["Hello", "World"]
10. trim() : String
문자열 양쪽 끝의 공백을 제거한다.
예를 들어 " Hello World "라면, 양쪽 끝의 공백을 제거하여, "Hello World"가 된다.
String strWithSpace = " Hello World ";
String trimmed = strWithSpace.trim(); // "Hello World"
11. isEmpty() : boolean
문자열이 비어 있는지 확인하는 데 사용된다.
문자열이 완전히 비어있을 때 "true"를 반환하고, 하나 이상의 문자를 포함하고 있을 때 "false"를 반환한다.
String str1 = "";
String str2 = "Hello";
boolean isStr1Empty = str1.isEmpty(); // true 반환
boolean isStr2Empty = str2.isEmpty(); // false 반환
12. length() : int
문자열의 길이, 즉 문자열에 포함된 문자의 수를 반환한다.
문자열이 비어 있으면 0을 반환한다.
배열의 length와 헷갈리지 말 것!
String str1 = "Hello";
String str2 = "";
int lengthStr1 = str1.length(); // 5 반환 ('Hello'는 5개의 문자를 포함)
int lenghtStr2 = str2.length(); // 0 반환 (비어있는 문자열)