
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基础教程栏目。
以上就是java推断字符串是不是包括某个字符的要领的细致内容,更多请关注ki4网别的相干文章!