小编给大家分享一下java怎么判断字符串包含某个字符,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
java判断字符串是否包含某个字符的方法:
一、contains方法
1:描述
java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列
2:声明
public boolean contains(CharSequence s)
3:返回值
如果此字符串包含返回true,否则返回false。
4:实例
public static void main(String[] args) {
String str = "abc";
boolean status = str.contains("a");
if(status){
System.out.println("包含");
}else{
System.out.println("不包含");
}
}
二、indexOf方法
1:描述
java.lang.String.indexOf() 的用途是在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符。
2:声明
int indexOf(int ch,int fromIndex)
3:返回值
indexOf的返回值为int
4:实例
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怎么判断字符串包含某个字符”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。