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);
}
编译之后的输出:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 第一条返回结果是: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;

更多精彩