java 判断字符串是否包含子串的方法
方法一:
String str1 = "nihaoksdoksad "; String str2 = "ok "; int total = 0; for (String tmp = str1; tmp != null&&tmp.length()> =str2.length();){ if(tmp.indexOf(str2) == 0){ total ++; } tmp = tmp.substring(1); } System.out.println(str1+ "中含有 "+total+ "个 "+str2);
方法二:
String str1 = "nihaokokosdokosad "; String str2 = "oko "; int total = 0; for (String tmp = str1; tmp != null&&tmp.length()> =str2.length();){ if(tmp.indexOf(str2) == 0){ total ++; tmp = tmp.substring(str2.length()); }else{ tmp = tmp.substring(1); } } System.out.println(str1+ "中含有 "+total+ "个 "+str2);
方法三:
String str1 = "nihaoksdoksad "; char []c=str1.toCharArray(); int total=0; for(int i=0;i <c.length-1;i++) if(c[i]== 'o '&&c[i+1]== 'k ') total++; System.out.println(str1+ "中含有 "+total+ "个ok ");
以上就是java 字符串判断是否含有子串的方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!