方法一:
1
2
3
4
5
6
7
8
9
10
|
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); |
方法二:
1
2
3
4
5
6
7
8
9
10
11
12
|
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); |
方法三:
1
2
3
4
5
6
7
|
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 字符串判断是否含有子串的方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/woshisap/article/details/6547420