개발언어/JAVA
[JAVA] startsWith, EndWith이란? 사용 방법과 예제
코딩 시그널
2020. 12. 1. 14:48
반응형
startsWith
- 문자열이 지정된 문자로 시작하는지 여부를 확인하는 메서드
사용방법
startsWith(String chars)
파라미터 : chars
반환 값(return)
- boolean
true - 문자열이 지정된 문자로 시작하는 경우
false - 문자열이 지정된 문자로 시작하지 않는 경우
예제
public class StartsWith {
public static void main(String[] args) {
String myStr = "Hello World";
System.out.println(myStr.startsWith("He")); // true
System.out.println(myStr.startsWith("he")); // false
System.out.println(myStr.startsWith("World")); // false
System.out.println(myStr.startsWith("e")); // false
System.out.println(myStr.startsWith("myStr")); // false
System.out.println(myStr.startsWith("Hello World")); // true
System.out.println(myStr.startsWith("HelloWorld")); // false
}
}
endsWith
- 문자열이 지정된 문자로 시작하는지 여부를 확인하는 메서드
사용방법
endsWith(String chars)
파라미터: chars
반환 값(return)
- boolean
true - 문자열이 지정된 문자로 끝나는 경우
false - 문자열이 지정된 문자로 끝나지 않는 경우
예제
public class EndsWith {
public static void main(String[] args) {
String endStr = "Hello World";
System.out.println(endStr.endsWith("ld")); // true
System.out.println(endStr.endsWith("World")); // true
System.out.println(endStr.endsWith("e")); // false
System.out.println(endStr.endsWith("myStr")); // false
System.out.println(endStr.endsWith("Hello World")); // true
System.out.println(endStr.endsWith("HelloWorld")); // false
}
}
아래 링크는 startsWith와 endsWith를 이용하여 알고리즘 풀이한 방법입니다.
[알고리즘] 프로그래머스 - 전화번호 목록 [해시]
아래의 풀이는 저의 풀이입니다. 이중 for문을 이용하고, substring을 이용해 각각의 항목들을 검사하는 방식으로 구현하였습니다. 좋은 답안은 아닙니다.... class Solution { public boolean solution(String[]..
junghn.tistory.com