containsCount
containsCount, function
def pure containsCount(haystack: text, needle: text): number
Returns the number of non-overlapping occurrences of the needle
within the haystack
.
haystack
: the full textneedle
: the text to be searched in thehaystack
Example
haystack = "Hello World!"
show summary "" a1c2 with
containsCount(haystack, "l") as "Contains 'l'?" // '3'
containsCount(haystack, "World") as "Contains 'World'?" // '1'
Errors
Needle is an empty string.
The needle
cannot be an empty text value.
Remarks
This function is case-sensitive.
Recipes and best practices
This function should not be used to get the number of fields in a text containing separators. You should use the fieldCount function to do that instead.
This function can be used to extend a table (can also be done at read with the “split” reading option)
table Items = with
[|as Description, as Colors |]
[|"pant", "blue,green,red" |]
[|"tshirt", "blue,orange" |]
[|"jacket", "red" |]
Items.Extend = if contains(Items.Colors, ",") then containsCount(Items.Colors, ",") + 1 else 1
table ItemsExt = extend.range(Items.Extend)
ItemsExt.Color = field(Items.Colors, ",", ItemsExt.N - 1)
show table "ItemsExt" with
ItemsExt.N
Items.Description
Items.Colors
ItemsExt.Color
This code returns:
N | Description | Colors | Color |
---|---|---|---|
1 | pant | blue,green,red | blue |
2 | pant | blue,green,red | green |
3 | pant | blue,green,red | red |
1 | tshirt | blue,orange | blue |
2 | tshirt | blue,orange | orange |
1 | jacket | red | red |