contains方法

Java String.contains()方法,当且仅当此字符串包含指定的char值序列时,返回true

public static void main(String[] args) {
String str1 = "abcdefg", str2 = "hijklmn";
CharSequence a = "abc";
boolean b = str1.contains(a);
System.out.println("第一条返回结果是 : " + b);
b= str2.contains("!");
System.out.println("第二条返回结果是: " + b);
}

编译之后的输出:

  第一条返回结果是:true

  第二条返回结果是:false

例如:从前端传过来name,如果name这个字符串中含有"j"就set到realName中,否则就set到Name中
if(user.getName().toLowerCase().contains("j")){
user.setRealName(user.getName());
}else{
user.setName(user.getName());
}
toLowerCase():不在区分大小写
contains():如果字符串中包含指定的char值序列时,返回true,否则就返回false;


 

 

原文地址:https://www.cnblogs.com/thcy1314/p/10687667.html