Javaで大文字小文字を無視して文字列に部分文字列が含まれているかどうかを確認するメソッド
Apache Commonsライブラリのorg.apache.commons.lang3パッケージのStringUtilsクラスは、 containsIgnoreCase()という名前のメソッドを提供します。 。
このメソッドは、ソース文字列と検索文字列をそれぞれ表す2つの文字列値を受け入れ、大文字と小文字を区別せずにソース文字列に検索文字列が含まれているかどうかを確認します。 −
であるブール値を返します-
ソース文字列に検索文字列が含まれている場合はtrue。
-
ソース文字列に検索文字列が含まれていない場合はfalse。
大文字と小文字を区別せずに、文字列に特定のサブ文字列が含まれているかどうかを確認するには-
-
次の依存関係をpom.xmlファイルに追加します
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
-
ソース文字列を取得します。
-
検索文字列を取得します。
-
上記の2つの文字列オブジェクトをパラメーターとして(同じ順序で)渡すことにより、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.
-
Java文字列に含まれるもの:ステップバイステップガイド
Javaのcontains() メソッドは、文字列に別の部分文字列が含まれているかどうかを確認します。 contains() メソッドはTrueまたはFalseを返します。 Java contains() 1つのパラメータを受け入れます:文字列で検索する部分文字列。 多くの場合、文字列を操作するときは、文字列に特定の文字シーケンスが含まれているかどうかを確認する必要があります。たとえば、これは、ユーザーの電話番号に米国ベースの国コードが含まれているかどうかを確認するアプリを作成している場合に発生する可能性があります。 Javaには、文字列に特定の値が含まれているかどうかを確認するた
-
文字列にSwiftの別の文字列が含まれているかどうかを確認します
文字列に別の文字列がswiftで含まれているかどうかを確認するには、2つの異なる文字列が必要です。別の文字列で構成されているかどうかを確認する必要がある1つの文字列。 チェックしたい文字列が「point」で、文字列全体が「TutorialsPoint」で、別の文字列が「onetwothree」であるとします。遊び場でこれら両方の文字列を確認しましょう。 これは、以下に示す2つの方法で実行できます。 3つの異なる文字列を作成することから始めましょう。 var CompleteStr1 = "Tutorials point" var completeStr2 = "