How many semicolons are in this note?

Suppose a writer wants to keep an eye on their fondness for the semicolon. They want an agent that collects notes with (say) more than two semicolons in the text.

How would you approach this? (I know one way, but it’s a little bit ugly. I bet there’s a better approach.).

That pattern in an agent query seems to be working as expected here.

$Text.contains(";.*;")

If you were going to use regular expressions, I think something like

$MyNumber=$Text.replace([^;],"").size

might do the trick. This deletes everything except semicolons, and then stores the number of semicolons in $MyNumber.

I think there’s a cleaner solution using the streaming interface…

Simplest might be to the String.countOccurrencesOf(literalStr) operator:

$MyNumber=$Text.countOccurrencesOf(";");

Also:

I can’t find that anywhere and it is not picked up by syntax colouring. But, this works:

$MyNumber=$Text.replace("[^;]","").size();

In case you are wondering, this pattern does pick up all semi-colons even if occurring as beginning or end characters of the source string. So string ";this;that;" would return a value of 3 for the above code ( i.e. the number of characters in string ";;;").

Note: do not be tempted to use .count() as that is counting the list size of the source string. This is slightly different from list.count() and list.size(), both of which count the number of list items.

1 Like

I completely forgot the countOccurencesOf() existed! Of course, that’s better than the streaming method, which is easy enough.


Oops on .length (C++) vs. .size() (Tinderbox).

1 Like

I too forgot String.countOccurencesOf(), until I started searching for .length :slight_smile:

(I’m reminded it arrived in v6.5.0 as a result of (mis-)using String.split() and string.contains() to get such counts.)