티스토리 뷰

반응형

1. length()

  • 문자열의 길이를 반환한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.length());  // 34 

2. toUpperCase() / toLowerCase()

  • 대소문자를 변환해준다.
  • ‘toUpperCase()’는 문자열 전체를 대문자로 변환해 준다.
  • ‘toLowerCase()’는 문자열 전체를 소문자로 변환해 준다.
String s = "I like Apple and Cheery and Banana";
//대문자로 변환
System.out.println(s.toUpperCase()); //I LIKE APPLE AND CHEERY AND BANANA

//소문자로 변환
System.out.println(s.toLowerCase()); //i like apple and cheery and banana

3. contains()

  • 대상 문자열에 특정 문자열이 포함되어 있는지를 확인해 준다.
  • 문자열이 포함되어 있으면 true 아니면 false를 반환한다.
  • 대/소문자를 구한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.contains("Apple")); //문자열이 포함되면 true
System.out.println(s.contains("Grapes")); //포함되지 않는 문자열이면 false

4. indexOf()

  • 문자열의 특정 문자나 문자열의 인덱스의 위치를 알려준다.
  • 문자열의 앞에서부터 처음발견되는 인덱스를 반환해 준다.
  • 문자열에 포함되지 않는 문장일 경우 ‘-1’을 반환한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.indexOf("Apple")); // 7
System.out.println(s.indexOf("Grapes")); // -1
System.out.println(s.indexOf("and")); //같은 단어가 있을때는 처음 일치하는 위치 정보(13)

5. lastIndexOf()

  • 문자열의 특정 문자나 문자열이 뒤에서부터 처음 발견되는 인덱스를 반환한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.lastIndexOf("and")); //마지막 일치하는 위치 정보(24)

6. startsWith()

  • 문자열이 입력된 문자열의 값으로 시작되는지 확인한다.
  • 입력된 문자열 값으로 시작하면 true 아니면 false를 반환한다.
  • 대/소문자를 구분한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.startsWith("I like")); // true

7. endsWith()

  • 문자열이 입력된 문자열의 값으로 끝나는지 확인한다.
  • 입력된 문자열 값으로 끝나면 true 아니면 false를 반환한다.
  • 대/소문자를 구분한다.
String s = "I like Apple and Cheery and Banana";
System.out.println(s.endsWith("a")); // true

8. repalce()

  • 문자열에서 특정한 문자를 원하는 문자로 변경하고 싶을 때 사용된다.
  • replace(”기존 문자열”, “변경할 문자열”)로 사용한다.
String s = "I like Apple and Cheery and Banana.";
System.out.println(s.replace(" and", ","));  //  I like Apple, Cheery, Banana.

9. substring()

  • 문자열의 특정 부분을 출력할 때 사용한다.
  • subString(int)로 사용하는 경우 시작위치부터 끝까지 출력된다.
  • subString(int, int)로 사용하는 경우 시작점부터 끝 위치 직전까지만 반환된다. 즉, 시작 위치의 문자는 포함하지만 끝 위치의 문자는 포함하지 않는다.
String s = "I like Apple and Cheery and Banana.";
System.out.println(s.substring(7)); // Apple and Cheery and Banana.
// index를 사용해서 출력하기 
System.out.println(s.substring(s.indexOf("Apple"))); //Apple and Cheery and Banana.
//시작 위치부터 끝 위치 직전까지만 반환된다.
System.out.println(s.substring(s.indexOf("Apple"),s.indexOf("."))); //Apple and Cheery and Banana

10. trim()

  • 문자열의 시작과 끝에 있는 공백을 제거할 때 사용한다.
  • 문자열이 중간 공백은 제거하지 않는다.
s = " I like   Bob        ";
System.out.println(s); // I like   Bob  
System.out.println(s.trim());//I like   Bob

11. concat()

  • 문자열을 결합할 때 사용한다.
  • concat(”결합할 문자”)로 사용한다.
String s1 = "Apple";
String s2 = "Banana";
System.out.println(s1+ ", "+ s2); //Apple, Banana
System.out.println(s1.concat(", ".concat(s2))); //Apple, Banana

12. equals()

  • 두 개의 문자열이 내용이 같은지를 비교할 때 사용한다.
  • 문자열 내용이 같으면 true를 다르면 false를 반환한다.
  • 대/소문자를 구분한다.
String s1 = "Apple";
String s2 = "Banana";
System.out.println(s1.equals(s2))  //false
System.out.println(s1.equals("Apple")) //true
System.out.println(s1.equals("Banana")) //false

13. equalsIgnoreCase()

  • 대소문자 구분 없이 문자열 내용이 같은지를 비교할 때 사용한다.
String s1 = "Apple";
System.out.println(s1.equalsIgnoreCase("apple")); //true

반응형