版权声明
1. 本站文章和资源均来自互联网收集和整理,本站不承担任何责任及版权问题。
2. 相关版权归作者及其公司所有,仅供学习研究用途,请勿用于商业目的。
3. 若侵犯您的版权,请发邮件至webmaster@ishare1.cn联系我们,我们确认后将立即删除。

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知识请关注java基础教程栏目。
爱分享




