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