How to I check whether a given character is within a string, well that's easy:

if (myString.Contains(myChar))
{
//TODO Logic
}

But, What I want is to get the index of the char |AND| get the index of the same char if it is in the word more than once.

So basically, i've got three variables:

string word = "USMAAN"
string wordTwo = "******"
char letter = "A";

So the compiler should do the following task:

First Index of the string word, It's "U", nope that doesn't == A, move on.
Second index of the string word, It's "S", nope that doesn't == A, move on.
Third Index of the string word, it's "M", nope that doesn't == A, move on.
Fourth index of the string word, it's "A", AHA! A == A, get the index.
Now change wordTwo to "***A**".
So on and so forth...

There might be many ways to implement this but, that's the problem I have at the moment.

I have two string and one char to test with....

How could I implement this?

Recommended Answers

All 6 Replies

The page has the different functionality of string member classes, I've already come across this before...

I just help in how to use these to solve the solution, or an idea/skeleton of how it may be solved.

At the moment i've done:

word.IndexOf(letter);

But that's not recursive, i.e. doesn't go through them all...

Maybe I need a forloop..ARGHH!

Now change wordTwo to "***A**".

First thing you need to learn is that you don't change strings. Strings are immutable, which means you can only create new ones, not change old ones. So you'll have to create a new string with the substituted value.

The simplest way to solve your problem is with a for loop and StringBuilder. Check each character and append the one you want to the StringBuilder, converting it to a normal string when you are done.

This is so troublesome >.<

It's 5 lines of code, counting closing } as a line, make it a method and it's 8.

This is so troublesome >.<

Luckily(or perhaps not) the first caveman who invented the fire and the wheel did not say that . . . ;)

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.