Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to count occurences of one text string within another text string?

Tags:

powerquery

m

Is there a simple way, using PowerQuery, to count the occurences of a text string within another text string?

For instance, if I have the string, "The quick brown fox jumps over the lazy dog and the lazy dog doesn't notice," how would I easily determine that the words lazy and dog occur twice?

I know I can use Text.Contains to determine whether lazy and dog occur within the string, but I don't know a simple way to determine how many times they occur.

like image 438
Marc Pincince Avatar asked Sep 01 '25 05:09

Marc Pincince


1 Answers

You can split the text, using the search word as delimiter. The number of list items minus 1, is the number of occurrences.

let
    String = "The quick brown fox jumps over the lazy dog and the lazy dog doesn't notice,",
    Count = List.Count(Text.Split(String,"dog"))-1
in
    Count
like image 58
MarcelBeug Avatar answered Sep 02 '25 20:09

MarcelBeug