java - find number letters in the string?

0 votes
29 views
asked May 12 in JAVA by anonymous
Hi

  how to find number occurence char in the string?

2 Answers

0 votes
answered May 12 by admin (8,115 points)

In Spring 

  int occurance = StringUtils.countOccurrencesOf("1.2.2.3", "2");

0 votes
answered May 12 by admin (8,115 points)
String testString = "a.b.c.d";
1) Using Apache Commons

int apache = StringUtils.countMatches(testString, ".");
System.out.println("apache = " + apache);
2) Using Spring Framework's

int spring = org.springframework.util.StringUtils.countOccurrencesOf(testString, ".");
System.out.println("spring = " + spring);
3) Using replace

int replace = testString.length() - testString.replace(".", "").length();
System.out.println("replace = " + replace);
4) Using replaceAll - 1

int replaceAll = testString.replaceAll("[^.]", "").length();
System.out.println("replaceAll = " + replaceAll);
5) Using replaceAll - 2

int replaceAllCase2 = testString.length() - testString.replaceAll("\\.", "").length();
System.out.println("replaceAll (second case) = " + replaceAllCase2);
6) Using split

int split = testString.split("\\.",-1).length-1;
System.out.println("split = " + split);
7) Using Java8 - 1

long java8 = testString.chars().filter(ch -> ch =='.').count();
System.out.println("java8 = " + java8);
8) Using Java8 - 2

long java8Case2 = testString.codePoints().filter(ch -> ch =='.').count();
System.out.println("java8 (second case) = " + java8Case2);
9) Using StringTokenizer

int stringTokenizer = new StringTokenizer(" " +testString + " ", ".").countTokens()-1;
System.out.println("stringTokenizer = " + stringTokenizer);
...