Skip to content Skip to sidebar Skip to footer

Java/android: Stringutils.countmatches Alternative For Android?

I want to count the number of times a character occurs in a string. I've found the one-liner: int count = StringUtils.countMatches('a.b.c.d', '.'); However, Android Studio doesn't

Solution 1:

I native java code you can use :

int count = s.length() - s.replace(".", "").length();

Very simple, but useful if you don't want to include other lib to your project.

Solution 2:

gradle:

   dependencies {
      ...
      implementation  'org.apache.commons:commons-lang3:3.0'
      ...
   }

java:

import org.apache.commons.lang3.StringUtils;

from now you should be able to use:

int count = StringUtils.countMatches("a.b.c.d", ".");

Solution 3:

Try this code for your integer variable.

int number = org.apache.commons.lang.StringUtils.countMatches(input, "a");

Follow this link :http://javarevisited.blogspot.in/2012/12/how-to-count-occurrence-of-character-in-String.html

Cheers!!

Post a Comment for "Java/android: Stringutils.countmatches Alternative For Android?"