Lua find number of occurences of searched string

1.6k Views Asked by At

I'm trying to count and print out the numeric value of how many times the "terminator" value has been outputted. Is there any function that does this? If not how should i tackle this problem?

output = get_application_name()
var = string.match("terminator", get_application_name())
print(var)

I would like to count

nil
terminator
nil
nil
terminaor
1

There are 1 best solutions below

4
hjpotter92 On

You can make use of the string.gsub function. From the docs:

gsub also returns, as its second value, the total number of matches that occurred.

So:

output = get_application_name()
_, count = output:gsub("terminator", '')
print(output)