Pardon me for not having an appropriate title.

I'm making a program which seems to be working so far - but, I've stumbled on a bit of a tricky dead end.

Basically, If I have a string or char variable, say:

string input = "H";

And I have another string variable, say:

string word = "olleH"

How do I check whether the content of the variable input is within variable word?

Pseudo Code:

Index 0 of word
Does that letter == input, if no, Next. If yes, *TODO Code*
Keep checking until reach end of list.

So i'm guessing i'll need a for loop and perhaps make the variable word in to an array and check every index. Would that work?

Recommended Answers

All 4 Replies

String.Contains

if (word.Contains(input)) {
    // yes it does
} else {
    // no it doesn't
}

Use the Contains method of the string class:

if (word.Contains(input)) 
   //other code here

That's all good and easy but I wish to get the index of the letter. So if the letter which I am looking for is 'H', and the word is olleH. Then the number I am looking for is 4.

String.IndexOf(String)

You do know you can read the documentation on String and see all this yourself?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.