How to check if a word appears in a sentence

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By aagoldberg

I’m trying to check if a user types a specific word in LineEdit. I have the word as a value in a dictionary, and I’ve used the command if word in new_text (users input), but that only check if the users input is equal to the word.

:bust_in_silhouette: Reply From: alfandango

You can try using a Regular expression to match the word in a sentence. I haven’t actually used Godot’s regex object myself but it does have one.

Alternatively, you would have to slice the sentence into a new array of words and then loop through each, compare to your dictionary to find a match.

:bust_in_silhouette: Reply From: hilfazer

Try these methods of String class

int find ( String what, int from=0 )
Finds the first occurrence of a substring. Returns the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed.

int find_last ( String what )
Finds the last occurrence of a substring. Returns the starting position of the substring or -1 if not found.

int findn ( String what, int from=0 )
Finds the first occurrence of a substring, ignoring case. Returns the starting position of the substring or -1 if not found. Optionally, the initial search index can be passed.