当前位置: 技术问答>java相关
在JAVA中如何实现在一个长字符串查找某个字符串??
来源: 互联网 发布时间:2015-06-16
本文导语: 在JAVA中如何实现在一个长字符串查找某个字符串?? 例如,我要在"abc,bci,ddd"中得知有没有"ddd"这个字符串,怎么实现?? 请指教 | 如果你是要匹配字符串,而不是判定它是否存在,好一点的...
在JAVA中如何实现在一个长字符串查找某个字符串??
例如,我要在"abc,bci,ddd"中得知有没有"ddd"这个字符串,怎么实现??
请指教
例如,我要在"abc,bci,ddd"中得知有没有"ddd"这个字符串,怎么实现??
请指教
|
如果你是要匹配字符串,而不是判定它是否存在,好一点的方法,你可以用正则表达式。
|
public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true.
Parameters:
str - any string.
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
NullPointerException - if str is null
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:
this.startsWith(str, k)
is true.
Parameters:
str - any string.
Returns:
if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws:
NullPointerException - if str is null