Search Unity

IndexOf 'n'th word in a string

Discussion in 'Scripting' started by Raptcha911, Mar 14, 2016.

  1. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    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?
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    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.
     
    Chrogo and Kiwasi like this.
  3. Darholm

    Darholm

    Joined:
    Mar 1, 2016
    Posts:
    63
    Hello,
    You can use String.IndexOf().
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This. Conceptually you want to look for the spaces between the words.
     
  5. TeamPixelGhost

    TeamPixelGhost

    Joined:
    Mar 13, 2016
    Posts:
    32
    Code (csharp):
    1. string Text = "This is not just a Text, but also a Test!!";
    2. //Split at space, comma and line break.
    3. string[] SplitText = Text.Split(new string[] {" ", ", ", "\r\n"}, StringSplitOptions.None);
    4.  
    5. //Then you can edit this text by calling SplitText[8], not 9 because it starts at 0
    6. //Edit to your liking and then just call something like this
    7. string tempText;
    8. for(int i=0; i < SplitText.Length; i++) {
    9. //Set the tempText to equal itself and the next part.
    10. tempText = tempText + SplitText[i];
    11. }
    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.
     
    Kiwasi likes this.
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    I think tempText in your example would end up being "ThisisnotjustaTextbutalsoaTest!!", though.
     
  7. TeamPixelGhost

    TeamPixelGhost

    Joined:
    Mar 13, 2016
    Posts:
    32
    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.

    Code (csharp):
    1. string Text = "This is not just a Text, but also a Test!!";
    2. //Split at space, comma and line break.
    3. string[] SplitText = Text.Split(new string[] {" ", ", ", "\r\n"}, StringSplitOptions.None);
    4.  
    5. //Then you can edit this text by calling SplitText[8], not 9 because it starts at 0
    6. //Edit to your liking and then just call something like this
    7. string tempText;
    8. for(int i=0; i < SplitText.Length; i++) {
    9. //Set the tempText to equal itself and the next part.
    10. tempText = tempText + " " + SplitText[i];
    11. }
     
  8. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    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?
     
  9. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    It should be in the String namespace, there's some examples here.
     
    Raptcha911 likes this.
  10. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    Sorry, I got it!!.. As soon as I posted (obviously :p).. Thanks a lot Boz0r.. It was simple enough..
     
  11. TeamPixelGhost

    TeamPixelGhost

    Joined:
    Mar 13, 2016
    Posts:
    32
    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.
     
  12. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    You mean a different array for each type of delimiter?.. Could you please elaborate?
     
  13. TeamPixelGhost

    TeamPixelGhost

    Joined:
    Mar 13, 2016
    Posts:
    32
    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.
     
  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Try out regular expressions.

    The \b anchor finds whole words:
    http://www.regular-expressions.info/wordboundaries.html

    Like this:
    Code (csharp):
    1.  
    2.             string str = "This is not just a Text, but also a Test!!";
    3.  
    4.             var rx = new Regex(@"\b(\w+)\b");
    5.             var matches = rx.Matches(str);
    6.             for(int i = 0; i < matches.Count; i++)
    7.             {
    8.                 var m = matches[i];
    9.                 Console.WriteLine(m.Value + " : " + m.Index);
    10.             }
    11.  
    note, this is using the System.Text.RegularExpressions namespace.
     
    Last edited: Mar 14, 2016
    TeamPixelGhost likes this.
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Alternatively you can just break on spaces and consider the comma to be the last character in the word.
     
  16. TeamPixelGhost

    TeamPixelGhost

    Joined:
    Mar 13, 2016
    Posts:
    32
    True, he might have to do another Split unless he doesn't mind the comma being highlighted too.