Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Comparing Strings - finding substring..

Discussion in 'Scripting' started by Driftter, May 30, 2011.

  1. Driftter

    Driftter

    Joined:
    Jan 12, 2011
    Posts:
    14
    Hi buddies,

    I've tried many different methods to compare two strings (a substring with a string, to be precise) with no avail. I have a fixed string and and i'm also getting keyboard input from user, and what i'm gonna do is to compare each input with main string via a loop to detect if user input is included in the fixed string or not, and if it's included, i want the exact index of the input at the fixedString.array.

    Here's my function:

    Code (csharp):
    1.  
    2.  
    3. function manageUserAnswers(_answer)
    4.     {
    5.                 //just assume that answer string is "abcdefg"
    6.                 var answer : string = "abcdefg";
    7.         for(var i = 0; i < answer.Length; i++)
    8.             {
    9.                 /* doesn't work
    10.                                 if(_answer == answer[i])
    11.                     {
    12.                         print("true pick at index: " + i);
    13.                     }
    14.                 else
    15.                     {
    16.                         print("no luck... try another word " + _answer + " " + answer[i]);
    17.                     }*/
    18.                
    19.                 /* doesn't work
    20.                 if(String.Equals(System.Convert.ToInt32(_answer), System.Convert.ToInt32(answer[i]) ))
    21.                     print("true");
    22.                 else
    23.                     print(i + " -- " + _answer + " and " + answer[i] + " not the same");
    24.                 */
    25.  
    26.                 /* this one works, but does not return the index of matched element
    27.                                 if(answer.IndexOf(_answer) > 0)
    28.                     print("true");
    29.                                 */
    30.            
    31.             }
    32.     }
    33.  
    34.  

    Thanks in advance for helping me on this.
     
  2. Driftter

    Driftter

    Joined:
    Jan 12, 2011
    Posts:
    14
    Update:

    just call that function with any string, like:

    manageUserAnswers("a");
    manageUserAnswers("x");

    ...
     
  3. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    (a substring with a string, to be precise)

    String.Contains IIRC

    Edit:

    Of you want the index... Then use:

    IndexOf

    http://pastebin.com/2uKYTeh1

    (C# console application).
     
    Last edited: May 30, 2011
  4. Driftter

    Driftter

    Joined:
    Jan 12, 2011
    Posts:
    14
    Ooops, i figured out where i was doing wrong. i forgot to convert stringArray's elements to string. so i was actually comparing two different types...

    Thanks NPSF3000..