方式一:使用indexOf()方法
String str = "专案,供应链,工程,IE,会计,经营管理,IT";
int result1 = str1.indexOf("IE");
if(result1 != -1){
System.out.println("字符串str中包含子串“ab”"+result1); //返回索引
}else{
System.out.println("字符串str中不包含子串“ab”"+result1);
}
输出结果
方式二:使用contains()方法
String str = "专案,供应链,工程,IE,会计,经营管理,IT";
boolean status = str.contains("工程"); //返回true或false
if(status){
System.out.println("包含");
}else{
System.out.println("不包含");
}
方式三:使用startsWith()方法
此方法定义的语法:
参数:
prefix -- 要匹配的前缀。
toffset -- 开始寻找字符串索引。
public boolean startsWith(String prefix, int toffset)
或
public boolean startsWith(String prefix)
String str = "专案,供应链,工程,IE,会计,经营管理,IT";
System.out.println(Str.startsWith("专案") ); //返回值为true或false
网友评论