Search Unity

Comparing two similar strings, but the result is negative always

Discussion in 'Scripting' started by amit.r007, Jun 1, 2013.

  1. amit.r007

    amit.r007

    Joined:
    Jun 1, 2013
    Posts:
    17
    I am reading data from a text file.
    The data is stored in text file as,

    1,A
    2,B
    3,C
    4,A
    5,B
    and so on......

    following is the code that I am using....


    Code (csharp):
    1. string[] str = Correct_Ans.text.Split("\n".ToCharArray());
    2. for (int i = 0; i < 10; i++) {
    3.                    
    4.             string[] qAns = str[i].Split(',');
    5.            
    6.             if(qAns[1] == "A") {
    7.                 GameConstants.AnswerArray[i+1] = 1;
    8.                
    9.             }
    10.             else if(qAns[1] == "B")
    11.                 GameConstants.AnswerArray[i+1] = 2;
    12.             else
    13.                 GameConstants.AnswerArray[i+1] = 3;
    14.             Debug.Log("Ans Array = " + GameConstants.AnswerArray[i+1] );
    15.            
    16.             Debug.Log("qAns[1] = " + qAns[1] + "Type = "+qAns[1].GetType());
    17.         }
    18.  
    The Debug stmt
    Debug.Log("qAns[1] = " + qAns[1] + "Type = "+qAns[1].GetType());
    gives proper output ie:
    qAns[1] = C Type = System.String

    but Debug.Log("Ans Array = " + GameConstants.AnswerArray[i+1] );
    gives output as : Ans Array = 3

    It always executes the else statement.

    I have used all possible functions for comparing these two strings but still the output is same....

    Please help.

    Regards,
    Amit.
     
    Last edited: Jun 1, 2013
  2. KaBKa

    KaBKa

    Joined:
    May 20, 2013
    Posts:
    34
    didnt look too closely at ur code but i did see one thing that got me a while back... its \r\n so you very well might be comparing A to A\r
     
  3. amit.r007

    amit.r007

    Joined:
    Jun 1, 2013
    Posts:
    17
    Gosh...

    Yes I was comparing A to A\n ........... :)

    Thanks for the help.

    I used
    Code (csharp):
    1. qAns[1]=qAns[1].Replace("\r", string.Empty).Replace("\n", string.Empty);
    and it works perfectly fine now...... :)