lama5
lama5

Reputation: 11

Lua find number of occurences of searched string

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

Upvotes: 0

Views: 1749

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

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)

Upvotes: 1

Related Questions