Checking if string contains another string

Started by Gepard, Wed 27/04/2011 17:07:34

Previous topic - Next topic

Gepard

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.
Drink up me 'arties! Yo ho!

Khris


Gepard

Drink up me 'arties! Yo ho!

Khris

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:

Code: ags
  int result = haystack.Contains(needle);

Gepard

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.
Drink up me 'arties! Yo ho!

monkey0506

#5
That's what variables are for.

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


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


Code: ags
// 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);


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


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


..etc., depending on implementation.

Khris

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().

monkey0506

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:

Code: ags
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.

Gepard

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.
Drink up me 'arties! Yo ho!

Khris

If the player chose the word from a listbox called lbWordlist:

Code: ags
  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.

Gepard

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???
Drink up me 'arties! Yo ho!

TomatoesInTheHead

Where do you have this strange "%s" idea from? ??? Just use
Code: ags
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...

monkey0506

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.

SMF spam blocked by CleanTalk