It is currently April 18th, 2024, 4:20 pm

Number of character or phrase included in a string

General topics related to Rainmeter.
mak_kawa
Posts: 908
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: 908
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: 908
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.
User avatar
Corvust
Posts: 7
Joined: June 5th, 2018, 7:15 pm

Re: Number of character or phrase included in a string

Post by Corvust »

You can achieve the same goal without LUA.

Code: Select all

[Variables]
String=This is a test string that has a lot of characters

[StringSubstitute]
Measure=String
String=#String#
RegExpSubstitute=1
DynamicVariables=1

; Substitute 'All characters, including white spaces'
Substitute="(.)":"9"

; Substitute 'All characters, excluding white spaces'
; Substitute="(\S)":"9"," ":""

[NumberOfCharacters]
Measure=Calc
DynamicVariables=1
Formula=Log([StringSubstitute]+1)

[Length]
Meter=String
MeasureName=NumberOfCharacters
FontColor=255,255,255
I posted this snippet on the subreddit a while back. The only limitation is it supports a maximum character length of 308.
Last edited by eclectic-tech on July 11th, 2020, 4:03 pm, edited 1 time in total.
Reason: Added a meter to make the code valid.
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Number of character or phrase included in a string

Post by balala »

Corvust wrote: July 11th, 2020, 3:46 pm The only limitation is it supports a maximum character length of 308.
This can be avoided with a code developed by Yincognito a few months ago. It is based on an extremely smart idea: to substitute each character with +1, then summing up those +1 values. The following code is his work, not mine, even if right now I am posting it.

Code: Select all

[Variables]
String=This is a test string that has a lot of characters

[StringSubstitute]
Measure=String
String=#String#
RegExpSubstitute=1
Substitute=".":"+1"

[NumberOfCharacters]
Measure=Calc
Formula=[StringSubstitute]
DynamicVariables=1

[Length]
Meter=String
MeasureName=NumberOfCharacters
FontColor=255,255,255
As you can see, no lua script involved and no upper limit. It can work even above 308 characters.
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Number of character or phrase included in a string

Post by mak_kawa »

Hi Corvust

Cool idea!... but you seem to be misunderstanding my aim. Maybe, my English was(is) bad. And, if I misunderstand your substitution way, sorry!

I want to count the number of a "specific" character (or phrase) included in a string, not a number of all character.

So, I have modified your RegExpSubstitute, and it works.

Code: Select all

[Variables]
String=I am a man -> But you are a woman -> Don't care
SearchString=->

[StringSubstitute]
Measure=String
String=#String#
RegExpSubstitute=1
Substitute="([^#SearchString#])":"","(#SearchString#)":"9"
DynamicVariables=1
* No-length-limitation idea by Yincognito which balala pointed is also cool.
** But honestly, I love the Lua way, sorry... :-)
Last edited by mak_kawa on July 11th, 2020, 8:20 pm, edited 1 time in total.
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Number of character or phrase included in a string

Post by mak_kawa »

Stupid me... above substitution doesn't work for some other case of SearchString. O.O
Further consideration is needed. What a RegExp noob! :-)
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Number of character or phrase included in a string

Post by balala »

mak_kawa wrote: July 11th, 2020, 8:06 pm I want to count the number of a "specific" character (or phrase) included in a string, not a number of all character.
Sorry I didn't read carefully enough your request and omitted this detail. So here is a workaround, still with no lua script involved. Just replace the Substitute option of the [StringSubstitute] measure with the following one: Substitute="[^#SearchString#]":"+0","#SearchString#":"+1".
Still no expression number limit problems.
Sorry for my inattention.
mak_kawa wrote: July 11th, 2020, 8:06 pm ** But honestly, I love the Lua way, sorry... :-)
I don't doubt it is possible with a lua script as well, but here someone else should help you.
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Number of character or phrase included in a string

Post by mak_kawa »

Hi balala

My Lua code shown above is simple but successful with no problem. I don't want any other. But if there occurs some problem in the Lua way, I would be helped by the RegExpSubstitution. :-)

BTW, above RegExpSubstitute uses the "character" class, not "word". Probably for this reason, it doesn't work with, for example, SearchString=man. Again, stupid me. I will examine the right way, if possible.
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Number of character or phrase included in a string

Post by mak_kawa »

I tried to modify RegExpSubstitute as;

Code: Select all

;For "9" substitute
Substitute="#SearchString#":"9","[^9]":""
;For "+1" substitute
Substitute="#SearchString#":"+1","[^+1]":""
But, of course as you know, these substitution don't work as intended if the target string itself contains "9", "+", and "1"...