반응형
JAVA에서 문자를 비교하는 방법은 여러가지가 있지만 대표적으로 비교하는
함수들(contains, indexOf, equals, matches)을 가지고 예제를 만들어 봤습니다.
public class study4 {
public static void main (String[] args) {
String comment = "Try not to become a man of success but rather try to become a man of value.";
//contains, indexOf, equals, matches
//contains 일치하면 true 반환
System.out.println("contains return1: " + comment.contains("success but"));
//contains 불일치하면 false 반환
System.out.println("contains return2: " + comment.contains("fail but"));
//indexOf 일치하면 true 반환
System.out.println("indexOf return1: " + (comment.indexOf("man of") > -1));
//indexOf 불일치하면 false 반환
System.out.println("indexOf return2: " + (comment.indexOf("woman of") > -1));
//equals 일치하면 true 반환
System.out.println("equals return1: " + comment.equals("Try not to become a man of success but rather try to become a man of value."));
//equals 불일치하면 false 반환
System.out.println("equals return2: " + comment.equals("Try not to"));
//matches 일치하면 true 반환
System.out.println("matches return1: " + comment.matches(".*Try not to.*"));
//matches 불일치하면 false 반환
System.out.println("matches return2: " + comment.matches("Try not to"));
}
}
결과
contains return1: true
contains return2: false
indexOf return1: true
indexOf return2: false
equals return1: true
equals return2: false
matches return1: true
matches return2: false
반응형
'Develope > JAVA' 카테고리의 다른 글
[JAVA] exe 파일 실행 (0) | 2019.06.10 |
---|---|
[LDAP] PartialResultException 해결책 (2) | 2019.06.04 |
[JAVA] 세션(session) 설정 (0) | 2019.05.19 |
[JAVA] 삼항연산자 (0) | 2019.05.19 |
[JAVA] JDK 환경변수 설정 (2) | 2019.05.12 |