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
830
Reaction score
325
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
830
Reaction score
325
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 Threads

Latest Posts

Latest Profile Posts

GameDevJohn wrote on whtdragon's profile.
Hey I used you bunny and cat spritesheet for a game jam game. Thanks so much! You have really beautiful stuff here. I'll credit you at https://gamedevjohn.itch.io/bunny-boy
The video of a psycho chasing people on the streets with a sword before getting subdued is wild. Apparent screenshots of his Twitter feed (EDIT: confirmed by Dallas police) before it went private reveal a dangerous delusional mindset. I heard there was a bow person too. The crazies are really coming out of the woodworks.
Stream will be live shortly with some Witcher 3, followed by a session of the Interactive Text Adventure! Feel free to drop by!
Managed to find a way to hang a bed cover over my window. So no more being toasted alive in the afternoon!:kaoswt2:
So, uh, has anyone tried buying any resources from KokoroReflections? Because I've been looking for a 'buy' button on the site for like 10 minutes now and can't find one.

Forum statistics

Threads
98,005
Messages
948,451
Members
129,256
Latest member
apoorv
Top