GameMaker: Studio

GameMaker: Studio

View Stats:
Scooter Oct 22, 2012 @ 4:59pm
How to find a word in a string?
I want to know if there is a certain word in a certain string. For example: If the string was "Hello World" and the word I want to find was "Hello", then it would return as true. But if the string was "Hellowold" and the word was still "Hello" It should return as false because it isnt a word with spaces. Is there a good way that I can find a certain word with spaces? And if its a one letter string like "Hello!" because its still a word but with no other words to make spaces?
< >
Showing 1-3 of 3 comments
Karmond Oct 22, 2012 @ 5:23pm 
First you could use string_count() to search for your substring. If positive, continue to use string_lettersdigits() to remove all punctuation and non-alphabetic characters (the first step is to ensure that nobody wrote something like 'He-_-ll?o'). Then you would use string_count() with a space before and after the substring AND string to check that the word wasn't part of another word. If positive, great, you have your word.

Does that work for you?

EDIT: I just realised that string_lettersdigits() might remove spaces. In which case you may need to make your own script to remove punctuation OR just use string_replace_all() to convert the spaces into something alphanumerical first, then use the string_lettersdigits() function and then string_replace_all() to get the spaces back (if you want to, you could still do the final check if you substitute the replacement for the spaces).
Last edited by Karmond; Oct 22, 2012 @ 5:52pm
[m00] insane_eon Oct 22, 2012 @ 7:36pm 
string_pos can find substrings for you. It'll return a nonzero index to the start of the substring if it exists at all. Note that characters are indexed from 1 in strings in GML. If you don't have too many conditions on characters delimiting your string, you can probably just search for all the permutations without too much overhead.
Type string_ into the index of the help file to see the string functions. Also contents->Reference->Strings
On windows you can use DLL extensions - you could use that to interface to a regex library of some kind (e.g. the one in boost) which would make this easy.

Scooter Oct 25, 2012 @ 5:41pm 
Thanks! :-D
< >
Showing 1-3 of 3 comments
Per page: 1530 50

Date Posted: Oct 22, 2012 @ 4:59pm
Posts: 3