언어/JAVA
java) String 클래스 메소드 정리
[Nada]
2019. 12. 8. 16:11
참고) jdk 문서 : https://docs.oracle.com/javase/9/docs/api/index.html?overview-summary.html
Java SE 9 & JDK 9
docs.oracle.com
- concat : 문자열 연결
1
2
3
4
5
6
7
8
9
10
11
|
public class SelfTestClass {
public static void main(String[] args) {
String st1 = "coffee";
String st2 = "bread";
String st3 = st1.concat(st2);
System.out.println(st3); // 결과: coffeebread
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
- substring(int beginIndex) , substring(int beginIndex, int endIndex) : 문자열 추출
1
2
3
4
5
6
7
8
9
10
11
|
public class SelfTestClass {
public static void main(String[] args) {
String st1 = "coffee";
System.out.println(st1.substring(2)); // 2번째 인덱스 ~ endIndex 까지 문자열 추출, 결과: ffee
System.out.println(st1.substring(2, 4)); // 2번째 ~ 3번째 인덱스만 추출 , 결과: ff
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
- equals : 문자열 내용 비교 , 같으면 true 다르면 false 반환
1
2
3
4
5
6
7
8
9
10
11
12
|
public class SelfTestClass {
public static void main(String[] args) {
String st1 = "StringCompare"; String st2 = "stringCompare";
if(st1.equals(st2))
System.out.println("두 문자열은 같음");
else
System.out.println("두 문자열은 다름"); // 대문자 S가 다르기 떄문에 다름
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
- compareTo(String anotherString)
- 두문자열 내용 일치하면 0 반환
- 호출된 인스턴스 문자열이 인자로 전달된 문자열보다 앞서면 0보다 작은 값 반환
- 호출된 인스턴스 문자열이 인자로 전달된 문자열보다 뒤서면 0보다 큰 값 반환
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class SelfTestClass {
public static void main(String[] args) {
String st1 = "StringCompare"; String st2 = "stringCompare";
int cmp;
cmp = st1.compareTo(st2);
if(cmp == 0)
System.out.println("두 문자열은 일치");
else if (cmp < 0)
System.out.println("st1 이 st2 보다 앞 선다: " + st1); // st1 S가 대문자 이기 때문에, 결과: st1 이 st2 보다 앞 선다: StringCompare
else
System.out.println("st2 가 st1 보다 앞 선다" + " " + st2);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
- compareToIgnoreCase(String str) : 대소문자 구분하지 않음 , 기능은 compareTo 와 동일
1
2
3
4
5
6
7
8
9
10
11
12
|
public class SelfTestClass {
public static void main(String[] args) {
String st1 = "StringCompare"; String st2 = "stringCompare";
if(st1.compareToIgnoreCase(st2) == 0)
System.out.println("st1 과 st2 는 일치"); // 해당 행 출력
else
System.out.println("불일치"); }
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |