cancel
Showing results for 
Search instead for 
Did you mean: 

Need to identify count of a character appearance in a string

amardeep2021
New Contributor III

Hi all,

I have a scenario where upstream document has a string field like below. In this string ‘:’ can appear for some records. All I need to find is how many times ‘:’ appears in the string for each record to pass the count.

Eg:
fjhfjh:jfhjhgg:oueru:nvmcb
aklkl:pqooo:
pqos:jfh
akkk
uryt:jdf:cvmb

Expected count result:
3
2
1
0
2

I have tried with available find, contain, indexof expressions. Not suits for this scenario. Can someone have a better trick to find count of a character instances in a string? Kindly help.

Regards,
amardeep

3 REPLIES 3

siwadon
Employee
Employee

I think $inputString.split(':').length - 1 should work.

bojanvelevski
Valued Contributor

Using regex is also an option:

$inputString.match(/[:]/g).length

amardeep2021
New Contributor III

Thanks all. Let me give a try and get back on this.