除了使用.contains
我认为最好的方法之外,您似乎还想构建一个数据结构并通过它进行搜索。要执行此类操作,您需要执行以下操作:
String str = "This.is.a.great.place.too.work.";
String[] splitWords = str.split("\\."); //The . is a special character in regex language, thus needs to be escaped. Alternatively you could use \\W which will split the string where ever it finds a character which is not a letter, digit or underscore.
List<String> words = Arrays.asList(splitWords);
System.out.println(words.contains("place"));
或者,您可以使用如下indexOf(String str)
方法:
if (str.indexOf("place") > 0)
{
System.out.println("String Exists");
}