Check a string stored in a variable.

Status
Not open for further replies.

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Hi!
How can I check how many occurrences of a word I have in a string stored in a variable?
I have a plugin that can store a free input txt in a variable.
I'm using this script (in Conditional Branches) to check what i wrote ($gameVariables.value(N).toLowerCase().includes("word")).
But how can I check how many time "word" recurs in the string?
Any ideas?
Thanks^^
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
831
Reaction score
326
First Language
Javascript
Primarily Uses
RMMV
How can I check how many occurrences of a word I have in a string stored in a variable?
Checks for the word apple in variable #1 and strores the number of how many matches in variable #2
Code:
$gameVariables.setValue(2, ($gameVariables.value(1).match(/apple/g) || []).length)
eg.
if Variable #1 was "apple apple orange apple grape banana lemon" the above would return 3
 

Mark91

Veteran
Veteran
Joined
May 19, 2017
Messages
56
Reaction score
6
First Language
Italian
Primarily Uses
RMMV
Checks for the word apple in variable #1 and strores the number of how many matches in variable #2
Code:
$gameVariables.setValue(2, ($gameVariables.value(1).match(/apple/g) || []).length)
eg.
if Variable #1 was "apple apple orange apple grape banana lemon" the above would return 3
Thank you, sir!
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
831
Reaction score
326
First Language
Javascript
Primarily Uses
RMMV
Thank you, sir!
No prob :)

However there is a flaw in that method...
what if you wanted to use a variable instead of the hardcoded word 'apple'...

...well never fear got that covered too ;)
Created a very small plugin for this :)

To use just add via the plugin manager:
1585844784227.png
Event:
1585844352102.png1585844731170.png
 

Attachments

Last edited:
Joined
Dec 16, 2017
Messages
110
Reaction score
280
First Language
English
Primarily Uses
RMMV
CT, coming through with the js help, as usual. You're awesome!
 

mlogan

Global Moderators
Global Mod
Joined
Mar 18, 2012
Messages
14,659
Reaction score
8,086
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Posts

Latest Profile Posts

Anyone have advice for recruiting friends to bugtest with you? I had some buddies volunteer without me asking, and we agreed on dates and times... and they all just flaked...
A mousefolk sprite I made, I think I did good.
Discovering math. functions for damage formulas was one of the greatest things for me.
It's very useful for balancing skills to work in a variety of possible circumstances.
For example if you have a skill that is supposed to work for both mages and warriors:
Code:
Math.max(a.atk * 2 - b.def, a.mat * 2 - b.mdf)
SpaceX: First-ever successful (manned) commercial spaceflight!! In these terrible and self-sabotaging times for the world, that's a ray of excitement and hope I needed badly.

Forum statistics

Threads
98,019
Messages
948,579
Members
129,276
Latest member
abhikhurana
Top