统计单个字符 char类型
方法一 char类型
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; char searchChar = 'e'; //统计单个字符 char类型 int countChar = charCount(someString, searchChar); System.out.println(countChar); } //统计单个字符 char类型 private static int charCount(String someString, char searchChar) { int count = 0; for (int i = 0; i < someString.length(); i++) { if (someString.charAt(i) == searchChar) { count++; } } return count; } }
方法二 String类型
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; String searchString = "e"; int count4 = oneStringCount(someString, searchString); System.out.println(count4); } // 单字符统计 public static int oneStringCount(String string, String subString) { int index = 0; int count = 0; while ((index = string.indexOf(subString, index)) != -1) { //在循环控制的条件中将获得的索引值赋给index,不等于-1是因为.在JDK中规定了indexOf查找不到子字符串时就返回-1.在我们这里也就是跳出循环的意思 index++;//得到索引后,从本位置开始进行下一次循环,所以字符串的索引加一 count++;//计数器统计出现的次数 } return count; } }
方法三 String类型递归方式获取
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; String searchString = "e"; int count = countOccurences(someString, searchString, 0); System.out.println(count); } private static int countOccurences(String someString, String searchString, int index) { if (index >= someString.length()) { return 0; } int count = someString.substring(index, index + 1).equals(searchString) ? 1 : 0; return count + countOccurences(someString, searchString, index + 1); } }
方法四 char类型递归方式获取
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; char searchChar = 'e'; int count = countOccurences(someString, searchChar, 0); System.out.println(count); } private static int countOccurences( String someString, char searchedChar, int index) { if (index >= someString.length()) { return 0; } int count = someString.charAt(index) == searchedChar ? 1 : 0; return count + countOccurences(someString, searchedChar, index + 1); } }
方法五 使用 Java 8 功能
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; char searchChar = 'e'; int count = stringCount(someString, searchChar); System.out.println(count); } private static long stringCount(String someString, char searchedChar){ long count = someString.chars().filter(ch -> ch == searchedChar).count(); return count; } }
多字符统计
方法1
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; String searchString = "el"; int count = stringCount(someString, searchString); System.out.println(count); } private static int stringCount(String someString, String searchString) { int count = 0; while (true) { int index = someString.indexOf(searchString); if (index != -1) { count++; someString = someString.substring(index + searchString.length());//每次截取不包含出现过的字符 // someString = someString.substring(index+1);//一个一个字符往后计算 } else { break; } } return count; } }
方法2
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; String searchString = "el"; int count = stringCount(someString, searchString); System.out.println(count); } private static int stringCount(String someString, String searchString) { int old_length = someString.length(); String replace = ""; if (someString.contains(searchString)) { replace = someString.replace(searchString, "");//将需要查找的字符串替换为空 } int new_length = replace.length();//用原来字符串的长度去减替换过的字符串就是该字符串中字符出现的次数 int count = (old_length - new_length) / (searchString.length());//因为是字符串中字符出现的次数,所以要除以字符串你的长度最后就是字符串在另一个字符串中出现的次数 return count; } }
方法3
public class searchStringCount { public static void main(String[] args) { String someString = "el1elwwelelelewwelwelelele"; String searchString = "el"; int count = stringCount(someString, searchString); System.out.println(count); } private static int stringCount(String someString, String searchString) { int countString = 0; if (someString.contains(searchString)) { if (someString.equals(searchString)) { countString = 1; } else { String[] splitString = someString.split(searchString); if (splitString.length > 0) { countString = splitString.length - 1; } } } return countString; } }