Is there a way to count the number of times a character occurs in a string?

I read through the string documentation but could not find how to do this.    Is there a way to do this? Thank you
2 answers

Yes, there is a way and is very easy, the steps are the next:

  1. You need to use the replaceAll function to replace all the chars that match with the char that you are looking for in your string
  2. You find the length of the original string and then perform a substraction between that length and the length of your new string that does not contain the searched char anymore

Original String = ‘Hello World’, lets suppose I am looking how many ‘o’s that string has
Char = ‘o’
String without chars = ‘Hell Wrld’
Original length – New string length = 11 – 9 = 2 ← This is the count of chars ‘o’ in ‘Hello World’

I will add some pictures of a microflow in case it is helpful

  1. Microflow CountChars in big picture


  1. Then StrWithoutChar looks like this

  1. And CharsCount variable looks like this, as you can see, in variables area the value is 2, which is correct in this example to count ‘o’s in ‘Hello World’

IMPORTANT NOTE: This example is case sensitive, if you want it to be case insensitive convert your string and char first to uppercase or lowercase using functions toLowerCase or toUpperCase, whatever you prefer.
Hope it helps



Hi Steffi


I have found this Java Action that you could use to count the occurence of a character in a string:


public class CountOccurences
  public static void main(String args[]) 
  String input = "aaaabbccAAdd";
  char search = 'a';             // Character to search is 'a'.
  int count=0;
  for(int i=0; i<input.length(); i++)
      if(input.charAt(i) == search)


Instead of System.out.println you could use “return count;” to use the given amount in a microflow