It is currently July 7th, 2020, 3:29 am

Number of character or phrase included in a string

General topics related to Rainmeter.
mak_kawa
Posts: 717
Joined: December 30th, 2015, 9:47 am

Number of character or phrase included in a string

Post by mak_kawa »

Sorry for non-specific question.

Is there a way to know the number of a character (or phrase) included in a string? Of course in Rainmeter skin.
Example: the number of "e" is 4 in the string variable #target#="I made a question here".

Need Lua script? gfind? gmatch? I don't know how, sadly.
Last edited by mak_kawa on June 12th, 2020, 11:17 pm, edited 3 times in total.
mak_kawa
Posts: 717
Joined: December 30th, 2015, 9:47 am

Re: Character or phrase number

Post by mak_kawa »

Possibly I have figured out...but not sure. So far I got "4" from this lua for the example in my above post.

Code: Select all

function Update()

count = 0
Target=SKIN:GetVariable('TargetStr')
Search=SKIN:GetVariable('SearchStr')

for i in string.gmatch(Target, Search) do
   count = count + 1
end

return count

end
I don't know whether this is a right way or not...
mak_kawa
Posts: 717
Joined: December 30th, 2015, 9:47 am

Re: Character or phrase number

Post by mak_kawa »

Now I have figured out (hope so) for my real skin using a lua script as;

Code: Select all

function Update()

count = 0
Target=SKIN:GetMeasure('WarningTextToSearch')
TargetString=Target:GetStringValue()
Search=SKIN:GetVariable('WarningPhraseSearch')

for i in string.gmatch(TargetString, Search) do
   count = count + 1
end

return count

end
My lua skill is really the same as baby...? :-)
But so far, my skin works as expected with this script.