To count the number of commas

Hi guys, I want to count the number of commas in a field, what should I do?

In the image I have 4 commas, but I want it to count when it reflects more or less than 4

This is the only way I could think of but it has it’s limitations.

What you could do is check up to a certain number (like 10) and see if there is a string after splitting on a comma.

ifelse(split({string},β€˜,’,10)<>β€˜β€™,10,split({string},β€˜,’,9)<>β€˜β€™,9,split({string},β€˜,’,8)<>β€˜β€™,8,split({string},β€˜,’,7)<>β€˜β€™,7,split({string},β€˜,’,6)<>β€˜β€™,6,split({string},β€˜,’,5)<>β€˜β€™,5,split({string},β€˜,’,4)<>β€˜β€™,4,split({string},β€˜,’,3)<>β€˜β€™,2,split({string},β€˜,’,1)<>β€˜β€™,1,0)

If you have 11 commas it will return 10. So you would need to go up to a number you’re comfortable with there not being extra commas.

I wish there was a way to get the length of an array / list but there is not.

Hope this helps

1 Like

Hello Max, thank you very much, the function works well when there are commas, when the string does not have commas it returns an inconsistent value

Maybe you should wrap the calculation in an ifelse before to check if there is a comma.

ifelse(locate({string}, β€˜,’)>0,{calculated field},0)

1 Like

Yes, that’s how I was doing it, thank you very much, it was correct because I only need to count 9 commas maximum

A more generic approach to count the occurrence of a character or any (non-overlapping) strings within a field is:

strlen({field})-strlen(replace({field},'<SEARCH_STRING>',''))

In your case, you can count the number of commas in your Tags field using the following calculation:

strlen({Tags})-strlen(replace({Tags},',',''))
1 Like