请问如果有一种方法(java)检查字符串是否存在于长字符串中,请让我知道吗? 示例:检查字符串hello是否存在于此字符串中“abcdffgfghdshg hello asdf” 在这种情况下,应该返回true。:如何知道长字符串中是否存在单词?
最好的问候, 玛雅
请问如果有一种方法(java)检查字符串是否存在于长字符串中,请让我知道吗? 示例:检查字符串hello是否存在于此字符串中“abcdffgfghdshg hello asdf” 在这种情况下,应该返回true。:如何知道长字符串中是否存在单词?
最好的问候, 玛雅
这可以简单地通过使用完成包含方法
String orig="abcdffgfghdshghelloasdf";
if (orig.contains("hello"))
return true;
else
return false;
在Java中,你可以做,在许多方面,例如,通过使用从字符串方法类别:
包含方法 - 以CharSequence作为参数:
boolean checkContains(){
String orig="abcdffgfghdshghelloasdf";
return orig.contains("hello");
}
匹配方法 - 采用regex作为参数:
boolean checkMatches(){
String orig="abcdffgfghdshghelloasdf";
return orig.matches(".*hello.*");
}
他们两人都是非常快,所以这其中你会使用没有大的区别。
与园艺相比,它们都非常快速,但第一种方法可以比第二种方法快几个数量级,而且不容易出错。 – Malvolio
非常感谢,我不知道。 –
http://stackoverflow.com/q/2023792/1849366 –