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.
It is currently April 24th, 2024, 11:27 am
Number of character or phrase included in a string
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Number of character or phrase included in a string
Last edited by mak_kawa on June 12th, 2020, 11:17 pm, edited 3 times in total.
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Character or phrase number
Possibly I have figured out...but not sure. So far I got "4" from this lua for the example in my above post.
I don't know whether this is a right way or not...
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
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Character or phrase number
Now I have figured out (hope so) for my real skin using a lua script as;
My lua skill is really the same as baby...?
But so far, my skin works as expected with this script.
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
But so far, my skin works as expected with this script.
-
- Posts: 7
- Joined: June 5th, 2018, 7:15 pm
Re: Number of character or phrase included in a string
You can achieve the same goal without LUA.
I posted this snippet on the subreddit a while back. The only limitation is it supports a maximum character length of 308.
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
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.
Reason: Added a meter to make the code valid.
-
- Rainmeter Sage
- Posts: 16164
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Number of character or phrase included in a string
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
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Number of character or phrase included in a string
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.
* No-length-limitation idea by Yincognito which balala pointed is also cool.
** But honestly, I love the Lua way, sorry...
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
** 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.
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Number of character or phrase included in a string
Stupid me... above substitution doesn't work for some other case of SearchString.
Further consideration is needed. What a RegExp noob!
Further consideration is needed. What a RegExp noob!
-
- Rainmeter Sage
- Posts: 16164
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Number of character or phrase included in a string
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.
I don't doubt it is possible with a lua script as well, but here someone else should help you.
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Number of character or phrase included in a string
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.
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.
-
- Posts: 908
- Joined: December 30th, 2015, 9:47 am
Re: Number of character or phrase included in a string
I tried to modify RegExpSubstitute as;
But, of course as you know, these substitution don't work as intended if the target string itself contains "9", "+", and "1"...
Code: Select all
;For "9" substitute
Substitute="#SearchString#":"9","[^9]":""
;For "+1" substitute
Substitute="#SearchString#":"+1","[^+1]":""