Hello guys,
I am having an issue with comparing strings in C#. I used the simple:
if(str1 == str2){
doSomething();
}
But it’s saying it can’t compare types ‘string’ and ‘string[ ]’ with ‘==’. I looked in the forums and it appears that at one point you were able to, but now I can’t? I tried some casts ( ‘int’ to many periods, ‘string[ ]’ couldn’t convert ).
str1 and str2 are not both strings, one is an array of strings(hence string[ ]).
Either compare each element in the string one at a time or figure out why you are mistaking a group of strings with a single string.
It only said that ‘String’ was not in current context. Does that mean it’s an actual function and I just need to include ‘String’ in it somewhere? Or is that completely wrong?
What are you actually trying to do? Do you have an array of strings, and you’re trying to see if the string is in the array? If so, use Array.Contains.
Type string and type string array are two different types, but that is resolved easily enough, something like this should work. keep in mind that you need to include the system namespace for access to String
string[] strings = new string[10];
string otherString = "whatever";
foreach (string part in strings)
{
if (String.Equals(part, otherString))
{
//do something when its true
}
else
{
//do something when its not true;
}
}
You could also use linq. Although many of these responses are great, Linq will do this exact thing for us,. and its a nice clean one liner. All you need to do is include System.Linq.
In the background, it iwll still do its loop, but is optimised and clean.
using UnityEngine;
using System.Collections;
using System.Linq;
public class NewBehaviourScript : MonoBehaviour {
public void someFunc(){
string[ ] testArray = {“test1”, “test2”};
its just no one mentioned it, thats the one that comes to mind instantly maybe because im a java coder by nature… But my question I guess is what is the fastest way to compare strings? and maye if anyone knows why is compareTag() quicker than comparing manually, it must do something special.
String equality becomes complicated because there’s a bunch of different ways to compare strings. See String.Equals and the StringComparison enum. None of that is slower than what’s reasonable for what you’re asking for, but if you’re doing a culture-sensitive comparison when what you really mean is “do these two strings have the same bytes”, you’re being slower than you need to.
String.Equals defaults the culture to CurrentCulture, which means that the behaviour depens on what computer it runs on, and is slower than the Ordinal (byte-by-byte) comparison.
Note that unlike Java, C# can override ==, so in C# == for strings checks content (byte) equality, not reference equality like in Java. So in C#, str1 == str2 is the same as str1.Equals(str2, StringComparison.Ordinal).
So in the vast, vast, vast majority of cases, str1 == str2 is what you’re looking for. In Java, str1 == str2 is almost always a bug.
CompareTag knows that there’s a limited number of tags, and that there’s no reason to care about any of that stuff, so it just compares the tag’s hash codes.