java 字符串比较(比较指定字符串中是否包含指定字符)
java字符串比较可以用.contains()方法和.indexOf()方法,其中.contains()方法是对.indexOf()的封装,所以性能上肯定是.indexOf()要快些.
.contains()方法源码如下:
/**
* 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 <code>s</code>, false otherwise
* @throws NullPointerException if <code>s</code> is <code>null</code>
* @since 1.5
*/
public boolean contains(CharSequence
s) {
return indexOf(s.toString()) > -1;
}
.contains()例子如下:
if ("xxxx".contains( "x")) {
system.out.println("true");
}else{
system.out.println("false");
}
.indexOf()例子如下:
String str="ABC_001";
if(str.indexOf("ABC")!=-1){
System.out.println("包含");
}else{
System.out.println("不包含");
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: jstl EL表达式遍历Map