티스토리 뷰
반응형
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
반응형
'개발 공부 > JAVA' 카테고리의 다른 글
[JAVA] LinkedList (0) | 2024.08.07 |
---|---|
[JAVA] ArrayList (0) | 2024.08.05 |
[Java] 생성자, 기본 생성자, 매개변수가 있는 생성자 (0) | 2022.02.21 |
[Java] 오버로딩(overloading) (0) | 2022.02.17 |
[Java] static 메서드와 인스턴스 메서드 (0) | 2022.02.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 매개변수가 있는 생성자
- SQL
- 프로글개머스 `
- 호출스택
- java의 정석기초
- 객체 배열
- 메서드 간의 호출과 참조
- 기본 생성자
- 2차배열 예제
- JAVA 의 정석
- JAVA의 정석
- 생활코딩
- 두 수의 나눗셈
- 객체지향
- do-while문
- return문
- java의정석 기초
- 조건문 if
- 자바의 정석
- 객체지향 언어
- string 배열
- 클래스와 객체
- Lv.0
- 기본형/참조형 매개변수
- 인스턴스 메서드
- 프로그래머스
- 코테
- MySQL
- 객체의 구성요소
- Java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함