如何检查字符串是否包含包含空格的子字符串? [英] How to check if a string contains a substring containing spaces?

查看:130
本文介绍了如何检查字符串是否包含包含空格的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在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

这篇关于如何检查字符串是否包含包含空格的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆