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
826
Reaction score
324
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
826
Reaction score
324
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,656
Reaction score
8,083
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 Profile Posts

Any also thought it's too cringy to put the word "and you" in your credit as a special thanks?
Cause that's what I thought...
Everyone in the US right now: Please stay safe.
Symphony Celestia: Prophecy of the Falling Stars RPG Maker music pack WIP. Symphony Celestia is a new series that focuses on symphonic and orchestrated music to tell a story. Each story will include compositions that make up that story from beginning to end. Themes include: Battles, adventures, character themes, etc. Below is a video preview to the first story.

Jumping and gravitation 0.1 build - RPG Maker MV
It's ironic, but one of the many hilarious things done with Shia's Just Do It speech was to make a hype song, and....I know what it is.....but I still get inspired by it! I'm like..."Yes I can!!"

Forum statistics

Threads
97,975
Messages
948,246
Members
129,227
Latest member
HamadAhli
Top