Сount the number of vowels

Hello. Can you tell me how to count the number of vowels in a string?
I output them to an array: List(“a”,“e”,“i”,“o”,“u”,“y”) and try to find them using Contains(), but nothing happens.

Hi @Givi_B and Welcome to the Community :partying_face: !

You can do this with a formula such as this one :blush: :
(see the field [Vowel Count - 1] in the sample below)

thisRow.Name.Split("").Filter(
   CurrentValue.Contains("A","a","E","e","Y","y","U","u","I","i","O","o")
 ).Count()

The first step is to Split() by blank ("") the string to create a list of all the characters in that string and then filter that list (each character being stored within CurrentValue) to keep only the vowels (lowercases and uppercases here :blush: ).
All that’s left is to Count() the number of characters Filter() might return :blush:

Another possibility would be to use RegexReplace() with a formula looking like this :
(see the field [Vowel Count - 2] in the sample)

thisRow.Name.RegexReplace("[^AaEeYyUuIiOo]","").Split("").Count()

or this :
(see the field [Vowel Count - 3] in the sample)

thisRow.Name.RegexReplace("[^AaEeYyUuIiOo]","").Length()

In both formulas we ask RegexReplace() to find what is not a vowel (so consonants) in the text with this Regular Expression : [^AaEeYyUuIiOo] which will match anything in the text except the lowercase vowels and the uppercase ones leaving only the consonants which will then be replaced by blank ("") so all that’s left is the text are the vowels… (:sweat_smile: )
(Sorry, I’m not RegEx fluent :innocent: )

From there, you can either Split() the result from RegexReplace() by blank ("") to create a list of characters and then Count() the number of characters in that list or simply use Length() (which returns the length of a string) :blush:

Hope this helps :innocent:

4 Likes

Hey @Pch I am doing something similar but I want to count colored blocks == emojis. I have taken your approach however Split() turns them into this new character that strips the information.

How can I count the number of green blocks or red blocks?

Hi @Johg_Ananda :blush: !

I just wanted to let you know that I’ll get back at you as soon as I get back home :blush:
(I stumbled upon a similar emoji trouble very recently trying to do something else… So I think I might have something that could work for your issue)

1 Like

Hi (again) @Johg_Ananda :blush: !

So, long story short :innocent: : I discovered few days ago that emojis are particular in terms of “length”/“weight” (In UFT8, some characters can be “multiple units” behind the scene and take more spaces than other characters… This is the case for emojis) which makes splitting strings containing emojis a bit harder than usual (as you’ve seen, by trying to split these, you’ve exposed the fact that emojis “weight” 2 characters instead of only one … hence the presence of the double weird looking “stack” icon instead of the simple emoji you were expecting)

I almost never go into that direction but to solve your issue, the easiest way I’ve found to count how many green and red square emoji they are in your strings, was to use the hidden RegexExtract() :innocent:

And RegexExtract() outputs the portions of text that match the Regular Expression

As we can use RegEx without “breaking” the emojis, after that it becomes easier to count what needs to be counted :blush:

So, in the sample below, I’ve created a table to store the concerned emojis and I’ve added, in the callout on the canvas a multi-select lookup control so you’d be able to select the emojis you wish to count in those strings … (I just found this easier than having to use a ListCombine() in the formula to store the emoji to count)
(There’s also, in the callout, a simple named formula which will display the selected emojis)

And then, you’ll find a table ([Table]) where I’ve counted the numbers of green and red square emojis in the string you’ll find in the field [String Emojis] :blush:

The formula you’ll find in [RegexExtract - All in one] is this one :

[Select Emoji].Emoji.ForEach(
    thisRow.[String Emojis].RegexExtract(CurrentValue, "g")
  ).Filter(
    CurrentValue.IsNotBlank()
  ).ForEach(
    Concatenate(CurrentValue.Count(), " ", CurrentValue.Nth(1))
  ).Join(" | ")

What it does is :
It takes the list of the currently selected emojis in the multi select lookup control as text values ( [Select Emoji].Emoji) and for each emoji in that list (each emoji being stored as CurrentValue), it will take the string and from that string extract the emoji (the last input in RegexExtract(), the "g" is a RegEx Flag. "g" means global). Which returns a list of lists of extracted emojis (depending on the strings and the selected emojis… Sorry, I have no idea how to say this differently :sweat_smile: ).
Then, because all that matters are those lists of extracted emojis, we filter the list of lists to get rid of the potential blank values (See the result in the field [Step 1 - RegexExtract] :blush: )

After that, all that left is for each of the list in the list of lists (if there’s more than one) is to

Concatenate(CurrentValue.Count(), " ", CurrentValue.Nth(1))

Note that CurrentValue.Nth(1) is there to only take the first emoji from the extracted lists of emojis :blush: … You can see the difference between using CurrentValue.Nth(1) and not using it in the fields [Steps Result - RegexExtract] (where I used it) and [Step 2 - RegexExtract] (where I didn’t) :blush:

I’ve also added a variation of the formula in the field [RegexExtract - All in one 2] where it will return something like 0 🟥 | 0 🟩 instead of blank in the case where there was no emoji to extract from the string :blush:

I’m sorry I couldn’t find a “clean” solution here (and had to rely on a hidden formula) but I hope this helps :innocent:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.