0
点赞
收藏
分享

微信扫一扫

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("不包含");}

举报

相关推荐

0 条评论