java中怎么判断一个字符串中包含某个字符或字符串

2025-03-09 10:41:27
推荐回答(4个)
回答1:

/**
* Returns true if and only if this string contains the specified
* sequence of char values.
*
* @param s the sequence to search for
* @return true if this string contains s, false otherwise
* @throws NullPointerException if s is null
* @since 1.5
*/
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}

String str = "dfdf点点滴滴";
boolean b1 = str.contains("d");
boolean b2 = str.contains("点点滴滴");

回答2:

可以用String.indexOf();不包含的话返回-1

回答3:

用isContain 函数

回答4:

str.indexOf("");