RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java怎么判断字符串中是否有指定字符

java判断字符串是否含有指定字符的方法:

创新互联自2013年起,先为北关等服务建站,北关等地企业,进行企业商务咨询服务。为北关企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

1、使用contains方法判断

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

声明:public boolean contains(CharSequence s)

返回值:此方法返回true,如果此字符串包含,否则返回false。

示例:

public static void main(String[] args) {  
        String str = "abc";  
        boolean status = str.contains("a");  
        if(status){  
            System.out.println("包含");  
        }else{  
            System.out.println("不包含");  
        }  
    }

2、使用indexOf方法判断

java.lang.String.indexOf() 的用途是在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符。

声明:int indexOf(int ch,int fromIndex)

返回值:indexOf的返回值为int

示例:

public static void main(String[] args) {  
    String str1 = "abcdefg";  
    int result1 = str1.indexOf("a");  
    if(result1 != -1){  
        System.out.println("字符串str中包含子串“a”"+result1);  
    }else{  
        System.out.println("字符串str中不包含子串“a”"+result1);  
    }  
}

以上就是java判断字符串是否含有指定字符的方法的详细内容,更多请关注创新互联其它相关文章!


新闻名称:java怎么判断字符串中是否有指定字符
地址分享:http://sczitong.cn/article/ieidcd.html