Compare 2 Strings - ignore capitalization

Hi there,

I try to compare 2 strings like

 if (userSolutionText == photoCube.renderer.material.name)

The Materialname (upper lower case letters) is equal to the String (only upper case letters, since I´m using a font with only uppercase letters) the user needs to submit. But for the checking i need to convert the 2 strings both to uppercase letters only.

Is there a function in Unity for that?

What about a function like strtoupper in Javascript, i tried but i think I´m making a mistake somewhere. Could somebody give me a hint or code snippet :wink:

Thanks a lot
Marc

The best place to look for these kind of things is the Mono docs or the .NET MSDN (String Class (System) | Microsoft Learn).

userSolutionText.ToLower() will return the lowercase version of userSolutionText. photoCube.renderer.material.name.ToLower() will do the same for the material name. Comparing the results of these function calls should yield the desired result.

Edit: it appears you asked for upper, which obviously would require ToUpper() rather than ToLower() :wink:

wow that was quick !!

Thanks a lot i will try that out as soon as I´m sitting at my computer again.

The ToUpper function did it ! There is now only a slight problem with the string from the material.

As soon as I press the submit button (-> starts the checking of the strings) the Materialname gets an additional “(Instance)” to its name which changes the otherwise equal strings. Ergo string != string 2.

What would be the best approach to get rid of the part “INSTANCE” (upper after conversion) → String2 - (INSTANCE).

As you see the .NET stuff is still new to me so I´m sometimes a little lost there.

Thanks again for your help

there are various ways.

expand the shorter string with the instance part and check then. check if the shorter string is contained in the longer string, if the longer starts with the shorter and all alike :slight_smile:

same you would do when you had a printed dictionary in front of you actually :slight_smile:

Thanks for the advice. I tried the expansion of the shorter String and it works.