Apache commons 라이브러리의 org.apache.commons.lang3 패키지의 StringUtils 클래스는 containsIgnoreCase()라는 메서드를 제공합니다. .
이 메서드는 소스 문자열과 검색 문자열을 각각 나타내는 두 개의 문자열 값을 받아들이고 대소문자를 무시하고 소스 문자열에 검색 문자열이 포함되어 있는지 확인합니다. -
인 부울 값을 반환합니다.-
원본 문자열에 검색 문자열이 포함된 경우 true입니다.
-
소스 문자열에 검색 문자열이 포함되어 있지 않으면 false입니다.
대소문자에 관계없이 문자열에 특정 하위 문자열이 포함되어 있는지 확인하려면 -
-
pom.xml 파일에 다음 종속성을 추가합니다.
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
-
소스 문자열을 가져옵니다.
-
검색 문자열을 가져옵니다.
-
위의 두 문자열 객체를 매개변수로 전달하여 containsIgnoreCase() 메서드를 호출합니다(같은 순서로).
예시
D 디렉토리에 다음 내용이 포함된 sample.txt라는 파일이 있다고 가정합니다. -
Tutorials point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. At Tutorials point we provide high quality learning-aids for free of cost.
예시
다음 Java 예제는 사용자로부터 하위 문자열을 읽고 파일에 대소문자에 관계없이 지정된 하위 문자열이 포함되어 있는지 확인합니다.
import java.io.File;
import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
public class ContainsIgnoreCaseExample {
public static String fileToString(String filePath) throws Exception {
String input = null;
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sub string to be verified: ");
String subString = sc.next();
String fileContents = fileToString("D:\\sample.txt");
//Verify whether the file contains the given sub String
boolean result = StringUtils.containsIgnoreCase(fileContents, subString);
if(result) {
System.out.println("File contains the given sub string.");
}else {
System.out.println("File doesnot contain the given sub string.");
}
}
} 출력
Enter the sub string to be verified: comforts of their drawing rooms. File contains the given sub string.