일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- dfs
- component
- 그래프
- 프로그래머스
- 카카오블라인드
- 이분탐색
- 백준
- 코틀린
- 배열
- BFS
- activity
- 운영체제
- GIT
- 안드로이드
- Android
- 완전탐색
- 자바
- 문자열
- 동적계획법
- 세그먼트트리
- 문자열다루기
- 스택
- 코딩
- 생명주기
- 코딩테스트
- 알고리즘
- BOJ
- 다이나믹프로그래밍
- 분할정복
- 트리
- Today
- Total
HS_development_log
알고리즘 실용 - String 메서드 활용 본문
1. String
String은 Reference type. 즉, 객체입니다. 따라서 int, double 같은 기본 타입 변수와 달리 wrapper 클래스를 사용하지 않아도 메서드 사용이 가능합니다.
2. String 객체의 주요 메서드
* 시간 복잡도 : 문자열의 길이를 n이라 할 때, O(N). 밑에 후술 할 모든 메서드에 해당합니다.
- String.replace()
String str = "abcdef";
str = str.replace("abc","123");
System.out.println(str); // 출력 : 123def
String.replace 메서드는 인자로 "변경당할 문자열", "변경할 문자열"을 받은 뒤 그 결과를 String 객체로 반환합니다.
- String.toUpperCase(), String.toLowerCase()
String str = "abcdef";
str = str.toUpperCase();
System.out.println(str); // 출력 : ABCDEF
str = str.toLowerCase();
System.out.println(str); // 출력 : abcdef
String.toUpperCase, String.toLowerCase 메서드는 각각 String의 모든 문자를 대문자, 소문자로 변환한 뒤 그 결과를 String 객체로 반환합니다.
- String.contains()
String str = "abcdef";
System.out.println(str.contains("1")); // 출력 : false
System.out.println(str.contains("ab")); // 출력 : true
String.contains 메서드는 문자열을 인자로 받아서 호출한 String객체에 해당 문자열이 포함되어있는지 검사 후 결과를 Boolean 객체로 반환합니다.
- String.charAt()
String str = "abcdef";
System.out.println(str.charAt(0)); // 출력 : a
System.out.println(str.charAt(4)); // 출력 : e
String.charAt 메서드는 인자로 index를 받아서 해당 index에 해당되는 문자를 char형으로 반환합니다.
- String.trim()
String str = " abc ";
System.out.println(str.trim()); // 출력 : abc
String.trim 메서드는 시작과 끝 부분의 공백을 모두 제거한 뒤 String 객체로 반환합니다.
- String.substring()
String str = "abcdef";
System.out.println(str.substring(1)); // 출력 : bcdef
System.out.println(str.substring(2,4)); // 출력 : cd
1. 인자로 beginIndex 만 받을 경우 beginIndex(포함)부터 마지막 인덱스까지의 문자열을 String 객체로 반환합니다.
2. 인자로 beginIndex와 endIndex를 받을 경우 beginIndex(포함) ~ endIndex(미포함)까지의 문자열을 String 객체로 반환합니다.
- String.split()
String str = "1 2 3 4 5 6";
String[] split = str.split(" ");
System.out.println(Arrays.toString(split)); // 출력 : [1, 2, 3, 4, 5, 6]
인자로 정규표현식을 입력받습니다. 해당하는 정규표현식으로 문자열을 토큰화 한 뒤 String [] 객체를 반환합니다.
'Algorithm-이론' 카테고리의 다른 글
알고리즘 실용 - Wrapper 클래스 메서드 활용 (0) | 2020.08.12 |
---|---|
자바 Collections 라이브러리의 시간 복잡도 (0) | 2020.02.01 |
가장 큰 증가하는 부분 수열(LIS) (0) | 2020.01.17 |
알고리즘 - 수학 (0) | 2020.01.15 |
세그먼트 트리(Segment Tree) / Java (0) | 2020.01.13 |