判断String中包含某个字符串的个数,
分享于 点击 7899 次 点评:209
判断String中包含某个字符串的个数,
- public class StringTest {
- private static int counter = 0;
- public static void main(String[] args) {
- String str1 = "sd& ^^java***java(((java";
- String str2 = "java";
- int i = countStrNum(str1, str2);
- System.out.println("str1中包含 " + i+" 个str2");
- }
- /**
- * 判断str1中包含str2的个数
- * @param str1
- * @param str2
- * @return counter
- */
- public static int countStrNum(String str1, String str2) {
- if (str1.indexOf(str2) == -1) {
- return 0;
- } else if (str1.indexOf(str2) != -1) {
- counter++;
- countStr(str1.substring(str1.indexOf(str2) +
- str2.length()), str2);
- return counter;
- }
- return 0;
- }
- }
相关文章
- 暂无相关文章
用户点评