サブ文字列「テンプレート」(たとえば)がStringオブジェクト内に存在するかどうかを確認するにはどうすればよいですか?
大文字と小文字を区別するチェックでなければ、すばらしいでしょう。
大文字と小文字を区別しない検索の場合、indexOfの前の元の文字列とサブ文字列の両方でtoUpperCaseまたはtoLowerCaseを実行します
String full = "my template string";
String sub = "Template";
boolean fullContainsSub = full.toUpperCase().indexOf(sub.toUpperCase()) != -1;
正規表現を使用して、大文字と小文字を区別しないものとしてマークします。
if (myStr.matches("(?i).*template.*")) {
// whatever
}
(?i)は大文字と小文字を区別せず、検索語の両端の。*は周囲の文字と一致します( String.matchesは文字列全体で機能するため)。
indexOf()およびtoLowerCase()を使用して、サブストリングの大文字と小文字を区別しないテストを実行できます。
String string = "testword";
boolean containsTemplate = (string.toLowerCase().indexOf("template") >= 0);
String word = "cat";
String text = "The cat is on the table";
Boolean found;
found = text.contains(word);