cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Anonymous
Not applicable

How to count # of occurrences of a character inside a string in Flow?

Hi everyone, 

 

I am trying to make a fun what-personality-are-you-quiz by using Forms, SharePoint and Flow and I've run into a problem. 

 

Each question in Forms has four options and every option is equivalent to a letter value in a SharePoint List (e.g. "a", "b", "c" or "d") and after using an ODATA filter to get the right letters depending on answers from Forms and a Compose i get a string with the values:

 

a, a, b, a, c, a, c, d,

 

And noooooow, I would like be able to count how many occurances "a", "b", "c" and "d" has, to then check which letter has the most occurances. I am looking for a function like CountIf or something similar but can't find it in Flow?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User III
Super User III

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Anonymous 

 

I should have mentioned earlier that you need length - 1. So in your case for a,a,b it is going to split as follows.

 

[
"",
",",
",b"
]
 
Total three array cells.  Try to use
sub(length(split(variables('Text'),'a')), 1)
 
If there are no matches this will return zero.
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks

 


Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog

View solution in original post

4 REPLIES 4
Highlighted
Super User III
Super User III

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Anonymous 

 

There is no such count function in Flow expressions. You could try using the split expression and find the count using the length.

 

See below expression.

 

length(split(variables('Text'),'a'))
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks



Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog
Highlighted
Anonymous
Not applicable

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @abm,

 

Thank you for your swift response! I tried it but seem to get 1 to many? For example "a,a,b" shows "3". Any clue why? 🙂

 

Thank you!

Highlighted
Super User III
Super User III

Re: How to count # of occurrences of a character inside a string in Flow?

Hi @Anonymous 

 

I should have mentioned earlier that you need length - 1. So in your case for a,a,b it is going to split as follows.

 

[
"",
",",
",b"
]
 
Total three array cells.  Try to use
sub(length(split(variables('Text'),'a')), 1)
 
If there are no matches this will return zero.
 

If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

 

Thanks

 


Did I answer your question? Mark my post as a solution!

If you liked my response, please consider giving it a thumbs up


Proud to be a Flownaut!

Learn more from my blog

View solution in original post

Highlighted
Anonymous
Not applicable

Re: How to count # of occurrences of a character inside a string in Flow?

Hi again @abm,

 

Thank you - it works like a charm! Well done 🙂 However, I just can seem to understand what exactly the lenght() function is counting? Smiley LOL I have tried breaking it done function by function, but still don't understand how works.

 

Anyhow, thanks again!

Helpful resources

Announcements
firstImage

Super User Program Update

Three Super User rank tiers have been launched!

firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

Top Solution Authors
Users online (6,730)