美文网首页
Java之判断字符串中是否包含某个字符

Java之判断字符串中是否包含某个字符

作者: 兰觅 | 来源:发表于2020-09-12 17:11 被阅读0次

方式一:使用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

相关文章

网友评论

      本文标题:Java之判断字符串中是否包含某个字符

      本文链接:https://www.haomeiwen.com/subject/kpkbektx.html