Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gepard on Wed 27/04/2011 17:07:34

Title: Checking if string contains another string
Post by: Gepard on Wed 27/04/2011 17:07:34
Is it possible to check whether a string contains another string?

Formula " haystack.Contains ("s%", needle) " failed so I guess it is not possible to use reference to a string in another string. Is there any other way?

Just so you have clear idea what I am talking about. On one place in the game I decide that: String needle = "one"; and on another place I decide that: String haystack = "one two three five". Now I need to check whether there is a needle in a haystack. Can you help me please?

Thanks.

PS: Did search the forum.
Title: Re: Checking if string contains another string
Post by: Khris on Wed 27/04/2011 17:29:17
The function is called String.IndexOf (http://www.adventuregamestudio.co.uk/manual/String.IndexOf.htm).
Title: Re: Checking if string contains another string
Post by: Gepard on Wed 27/04/2011 17:51:43
Yes, but Im using 2.71 (or 72).
Title: Re: Checking if string contains another string
Post by: Khris on Wed 27/04/2011 20:12:43
Ah ok, it is a good idea to tell us that from the start :=
I didn't read your post properly though.

This should work:

  int result = haystack.Contains(needle);
Title: Re: Checking if string contains another string
Post by: Gepard on Wed 27/04/2011 20:30:38
Problem is, that it is always a different word that Im looking for. Not just needle. It depends on what player chooses from a certain list.
Title: Re: Checking if string contains another string
Post by: monkey0506 on Thu 28/04/2011 00:27:22
That's what variables are for.

// use a string-literal (no String variables)
int index = haystack.Contains("needle");


// use a String variable with an specific value
String needle = "needle";
int index = haystack.Contains(needle);


// use a String variable with a user-defined value, using Game.InputBox
String needle = Game.InputBox("What word are you looking for?");
int index = haystack.Contains(needle);


// use a String variable with a user-selected value, using a ListBox
String needle = lstNeedle.Items[lstNeedle.SelectedIndex];
int index = haystack.Contains(needle);


// use a String variable, based on an array value
String needle = needles[needleID];
int index = haystack.Contains(needle);


..etc., depending on implementation.
Title: Re: Checking if string contains another string
Post by: Khris on Thu 28/04/2011 02:11:37
Not to be rude or anything but, so what?

This doesn't have anything to do with how to look for a string in another string; that's just about being able to implement String.Contains().
Title: Re: Checking if string contains another string
Post by: monkey0506 on Thu 28/04/2011 02:45:01
In addition to the examples I provided above (and I've even added more), I also want to note (based on your first post) that you can't just add string formatting to any function that you want. If the function lists "..." as the final parameter of the function, then it can use string formatting; otherwise it can't. You can't add string formatting to custom functions either since you'd have no way to read back the additional parameters.

If a function doesn't list "..." as the final parameter, then you can use the String.Format function directly within the function call:

int index = haystack.Contains(String.Format("Use %s", player.ActiveInventory.Name));

And of course, there's absolutely no reason to use string formatting at all in order to pass a String variable into a String/const string parameter.

Khris has a somewhat fair point in that this isn't about whether or not a function like String.Contains exists, this question is about the usage of the function. And even moreso not just about the usage of this function, but about programming in general. This question ultimately boiled down to an extremely basic matter of programming logic, how to use variables, and how to pass variables into function calls.
Title: Re: Checking if string contains another string
Post by: Gepard on Fri 29/04/2011 15:13:32
Thanks both. Im still not sure how to do this though. Not to stick to the string "theory" :D Is there another way to choose a word in one part of the game and then see if that word is contained in a sentence later in the game?

Thanks.
Title: Re: Checking if string contains another string
Post by: Khris on Fri 29/04/2011 15:33:46
If the player chose the word from a listbox called lbWordlist:

  needle = lbWordlist.Items[lbWordlist.SelectedIndex];

As long as needle is a global String variable, you can search haystack at any later point in the game.

I'm still at a loss about what exactly your problem is.
Title: Re: Checking if string contains another string
Post by: Gepard on Fri 29/04/2011 21:32:14
Ok, now we have the "needle" defined. My character comes to NPC and starts a dialog with him. The NPC has a text string assigned with words like "one two three", lets call it haystack. Dialog-request-script will run differently if the "needle" is contained in that NPC´s text string or isnt. Right? But how to do it?

if (haystack.Contains ("%s", needle)) {} can´t be used so what else???
Title: Re: Checking if string contains another string
Post by: TomatoesInTheHead on Fri 29/04/2011 21:49:17
Where do you have this strange "%s" idea from? ??? Just use
if (haystack.Contains (needle)) {}
as said before (several times).

And if that doesn't work or not work for your purpose, you have to give us some more actual code, or error messages...
Title: Re: Checking if string contains another string
Post by: monkey0506 on Fri 29/04/2011 23:55:59
I don't mean to be offensive, but you've been provided with several examples of how to do this properly and you're still not grasping how to pass a simple variable as a parameter to a function. This is an extremely basic concept in programming, and if you can't grasp it you're going to have a very difficult time ahead of you.