IndexOf 'n'th word in a string

Hey I needed to know how to get the starting index of any word in a given string…

String S = “This is not just a Text, but also a Test!!”

I’m making a highlighting system for which I need to place a markup tag before and after a word to change its color… I need to do this one word at a time so that it looks like the currently spoken word is being highlighted…

How can I do this?

You can use String.Split() to get the words in an array, edit the words you’d like, and use String.Join() to splice them back together.

2 Likes

Hello,
You can use String.IndexOf().

This. Conceptually you want to look for the spaces between the words.

string Text = "This is not just a Text, but also a Test!!";
//Split at space, comma and line break.
string[] SplitText = Text.Split(new string[] {" ", ", ", "\r\n"}, StringSplitOptions.None);

//Then you can edit this text by calling SplitText[8], not 9 because it starts at 0
//Edit to your liking and then just call something like this
string tempText;
for(int i=0; i < SplitText.Length; i++) {
//Set the tempText to equal itself and the next part.
tempText = tempText + SplitText[i];
}

This might not work 100% I’m not a thousand percent sure on if I’ve done the coding right, but it should work or at least put you in the right direction.

1 Like

I think tempText in your example would end up being “ThisisnotjustaTextbutalsoaTest!!”, though.

Good point, I forgot to add this in, which should add spaces back in but remove any Grammar. Though to be fair you could just split Text three times with for loops to check if it’s a Space, Comma or Line Break and add into seperate string[ ] then at the end when you rebuild the sentence do the same thing to add them back in before hand.

string Text = "This is not just a Text, but also a Test!!";
//Split at space, comma and line break.
string[] SplitText = Text.Split(new string[] {" ", ", ", "\r\n"}, StringSplitOptions.None);

//Then you can edit this text by calling SplitText[8], not 9 because it starts at 0
//Edit to your liking and then just call something like this
string tempText;
for(int i=0; i < SplitText.Length; i++) {
//Set the tempText to equal itself and the next part.
tempText = tempText + " " + SplitText[i];
}

Thanks a lot for all the ideas… I tried the above and I’m finding a problem joining the edited array… I split the string using delimiters like " " and ", " and ". "… And then edited a particular word, but I cant find the Join() function for… I have used both System.Collections and System.Collections.Generic namespaces, but I still cant find this Join() function… Is it only for javascript arrays??.. If so can I use Javascript Arrays in my c# code?

It should be in the String namespace, there’s some examples here.

1 Like

Sorry, I got it!!.. As soon as I posted (obviously :P)… Thanks a lot Boz0r… It was simple enough…

The only issue with Join is if you use a comma you’ll have this: “This , is , a , test”. You’d be better off piecing the strings together yourself or even doing three Arrays to find out what has a comma, what has a fullstop, it’ll take a bit longer but be worth it in the end.

You mean a different array for each type of delimiter?.. Could you please elaborate?

Well depending on what your game/app is about, you might have more but you would need to check for a comma, a fullstop or a space to break up words. So one way you could do this would be to have a seperate string[ ] for each, then create a for loop to set them, otherwise you’ll end up with the entire sentence but split at wrong sections. You might have to index it so you know the placement of each word, but then at the end instead of .Join set a temporary string to each piece like shown above. It’s longer to do but in the future it’ll be worth it as you can just copy/paste segments to add in new checks and if there are any issues or errors you’ll just have to edit your code to match.

Try out regular expressions.

The \b anchor finds whole words:

Like this:

            string str = "This is not just a Text, but also a Test!!";

            var rx = new Regex(@"\b(\w+)\b");
            var matches = rx.Matches(str);
            for(int i = 0; i < matches.Count; i++)
            {
                var m = matches[i];
                Console.WriteLine(m.Value + " : " + m.Index);
            }

note, this is using the System.Text.RegularExpressions namespace.

1 Like

Alternatively you can just break on spaces and consider the comma to be the last character in the word.

True, he might have to do another Split unless he doesn’t mind the comma being highlighted too.