Comparison between two strings not working (Chinese/Japanese characters)

Hi everyone !

I’m currently working on a small application in which you have to translate given words into an inputfield.

I have a very simple code to compare 2 strings which is launched at each change in an inputfield, but it doesn’t work when my texts contains Chinese/Japanese characters.

did I miss something ?

    public void CheckAnswer()
    {
        if (inputField.text.Equals(answer, System.StringComparison.OrdinalIgnoreCase)) print("you did it !");
        else print("texts are not the same : Answer = " + answer + " inputfieldText = " + inputField.text);
    }

And as you can see in the attached screen, everything seems to be the same…

Thanks for the help ! :slight_smile:

Maybe use ‘CurrentCultureIgnoreCase’ instead of ‘OrdinalIgnoreCase’.
And if the current culture is not the correct one, then use the correct culture.

Thanks ! I just made the switch, but it’s not working. And I’m sorry, but I not sure what to do about the culture ?

The culture is the language C# will use for some operations.
It’s best seen when displaying a float. For example, ‘2.7’ will be displayed ‘2.7’ in English and ‘2,7’ in French (not that the separator is different).

It may also be important when comparing strings I suppose (never tried).
You could change the culture to Chinese. Here is how to change culture (change ‘ur’ to the code corresponding to the culture you want):

CultureInfo ci = new CultureInfo("ur");
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;

Note that you can also ignore the culture by using ‘InvariantCultureIgnoreCase’.

Also, are you sure you do not have some invisible characters which are different between the 2 strings?
You can ensure it’s not the case by using the debugger and look into each string manually, character per character.
You can also loop over the strings, and display the character values (not the actual characters, but their ASCII values), it may help you to understand what the problem is.

1 Like

As the “InvariantCultureIgnoreCase” didn’t work, I tried to convert my strings to int. It showed that there were no spaces, but line breaks caused by the fact that these strings were taken from a csv file.

I should have done this much earlier, the problem is now solved.
Thank you very much for the help !