如何检查字符串是否包含包含空格的子字符串?
假设我在java中有这样的字符串:
Say I have a string like this in java:
这是{my string:} ok
"this is {my string: } ok"
请注意,各个字符之间可以有任意数量的空格。如何检查上面的字符串以查看它是否只包含子字符串:
Note, there can be any number of white spaces in between the various characters. How do I check the above string to see if it contains just the substring:
{my string:}
"{my string: }"
非常感谢!
如果您想查看String是否包含另一个特定的字符序列,那么您可以做这样的事情:
If you are looking to see if a String contains another specific sequence of characters then you could do something like this :
String stringToTest = "blah blah blah";
if(stringToTest.contains("blah")){
return true;
}
您也可以使用匹配。有关匹配字符串的合适解释,我建议您查看正则表达式的Java Oracle教程:
You could also use matches. For a decent explanation on matching Strings I would advise you check out the Java Oracle tutorials for Regular Expressions at :
http://docs.oracle.com/javase/tutorial/essential/regex/index.html
干杯,
Jamie