이 글에서는 자바(Java)를 활용해 하나의 문자열 안에 특정 부분 문자열(substring)이 포함되어 있는지 확인하는 방법을 알아보겠습니다.
먼저 기본 개념을 간단히 정리해 보겠습니다. 문자열(String)은 하나 이상의 문자로 구성된 데이터 타입으로, 큰따옴표(“ ”)로 묶어 표현합니다. 그리고 문자열의 일부 또는 부분 집합을 부분 문자열(substring)이라고 부릅니다.
다음은 실제 동작 과정을 보여주는 예시입니다.
입력값
The first substring is defined as: Java The second substring is defined as: C++
기대 출력 결과
The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.
알고리즘
Step 1 - 시작 Step 2 - input_string, sub_string_1, sub_string_2 세 개의 문자열 변수를 선언한다 Step 3 - 각 변수에 값을 정의한다 Step 4 - .contains() 메서드를 사용하여 문자열에 부분 문자열이 포함되어 있는지 확인한다 Step 5 - 결과를 출력한다 Step 6 - 종료
핵심 포인트: contains() 메서드
자바의 String.contains() 메서드는 CharSequence 타입의 인수를 받아, 해당 부분 문자열이 대상 문자열 안에 존재하면 true, 존재하지 않으면 false를 반환합니다. 참고로 이 메서드는 대소문자를 구분하므로, 대소문자와 관계없이 확인하고 싶다면 toLowerCase()나 toUpperCase()로 문자열을 변환한 뒤 비교하는 것이 좋습니다.
예제 1: main 메서드에서 모든 로직 처리하기
첫 번째 예제에서는 모든 연산을 'main' 메서드 안에서 한 번에 수행합니다.
public class Demo {
public static void main(String[] args) {
String input_string = "Java Programming";
System.out.println("The input string is defined as: " +input_string);
String sub_string_1 = "Java";
System.out.println("The first substring is defined as: " +sub_string_1);
String sub_string_2 = "C++";
System.out.println("The second substring is defined as: " +sub_string_2);
boolean result = input_string.contains(sub_string_1);
if(result) {
System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
} else {
System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
}
result = input_string.contains(sub_string_2);
if(result) {
System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
} else {
System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
}
}
}
출력 결과
The input string is defined as: Java Programming The first substring is defined as: Java The second substring is defined as: C++ The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.
예제 2: 객체 지향 방식으로 메서드 분리하기
두 번째 예제에서는 연산 로직을 별도의 메서드로 캡슐화하여 객체 지향 프로그래밍 스타일로 코드를 구성합니다. 이렇게 하면 코드의 재사용성과 가독성이 크게 향상됩니다.
public class Demo {
static void check_substring(String input_string, String sub_string_1, String sub_string_2){
boolean result = input_string.contains(sub_string_1);
if(result) {
System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
} else {
System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
}
result = input_string.contains(sub_string_2);
if(result) {
System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
}else {
System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
}
}
public static void main(String[] args) {
String input_string = "Java Programming";
System.out.println("The input string is defined as: " +input_string);
String sub_string_1 = "Java";
System.out.println("The first substring is defined as: " +sub_string_1);
String sub_string_2 = "C++";
System.out.println("The second substring is defined as: " +sub_string_2);
check_substring(input_string, sub_string_1, sub_string_2);
}
}
출력 결과
The input string is defined as: Java Programming The first substring is defined as: Java The second substring is defined as: C++ The substring: Java is a part of the defined string. The substring: C++ is not a part of the defined string.