Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LUA: count occurrences of a character into a string?

Tags:

lua

How can I count the occurrences of a specific character into a string, with LUA, please?

I give here an example

let the string: "my|fisrt|string|hello"

I want to count how many occurrences of the character "|" has the string. In this case, it should return 3

How can I do it please?

like image 476
Tormy Van Cool Avatar asked Oct 25 '25 09:10

Tormy Van Cool


1 Answers

gsub returns the number of operations in the second value

local s = "my|fisrt|string|hello"

local _, c = s:gsub("|","")
print(c) -- 3
like image 135
Mike V. Avatar answered Oct 28 '25 02:10

Mike V.