String.Contains Text Parser

Started by C.T.C.B, Tue 28/05/2013 23:45:37

Previous topic - Next topic

C.T.C.B

[embed=425,349]
How would I use String.Contains with a Text Parser?
For example if the player types in "Hello My name is John", I want it to check if the it contains "Hello" and then Display "Hello"
[/embed]

Phemar

#1
You would use String.IndexOf (String needle);

Eg:
Code: ags
String input = "Hello World";

if (input.IndexOf("hello") != -1)
  Display ("Hello!");

else
  Display ("Input does not contain hello.");


Edit: Then again, if you're using a text parser you wouldn't need to use IndexOf, you just use Parser.ParseText.

Eg:
Code: ags
String input = Game.InputBox ("Enter Text:");

Parser.ParseText (input);

if (Parser.Said("hello"))
  Display ("Hello!");

else
  Display ("Input does not contain Hello.");


This is of course assuming "Hello" isn't an ignored word :D

C.T.C.B

I guess that wasn't the best example since I want it so If the player types something before "Hello" such as "Also, let me just say hello first".

Khris

Since you must feed the text typed by the player into the parser manually, what's keeping you from using String.IndexOf first?

Code: ags
  String input = txtParserInput.Text;

  // first, do some manual parsing
  if (input.indexOf("hello") >= 0) {
    // input contains "hello"
    ...
    return; // exit
  }

  // now, AGS parsing
  Parser.ParseText(input);
  if (Parser.Said("...")) ...

Stupot

What if the player says something that contains the word 'hello' but does not necessarily warrant a greeting in return.
For Example:
"Hello, My name is John."
"Hello."
"I love Hello Kitty."
"Hello"
"No, I already said hello to you."
"Hello"
"Grrr, Goodbye you stupid Parser!"
"Goodbye *trolololol*"
MAGGIES 2024
Voting is over  |  Play the games

selmiak


SMF spam blocked by CleanTalk