Game Development Reference
In-Depth Information
//Compare strings
public bool IsSame(string Str1, string Str2)
{
//Ignore case
return string.Equals(Str1, Str2,
System.StringComparison.CurrentCultureIgnoreCase);
}
More information on the String.Compare method can be found online
in MSDN at http://msdn.microsoft.com/en-us/library/
system.string.compare%28v=vs.110%29.aspx .
Another method to quickly and regularly compare the same two strings for equality
is to use string hashes, that is, to convert each string into a unique integer and then to
compare the integers instead, as shown in the following code sample 6-13:
//Compare strings as hash
public bool StringHashCompare(string Str1, string Str2)
{
int Hash1 = Animator.StringToHash(Str1);
int Hash2 = Animator.StringToHash(Str2);
return Hash1 == Hash2;
}
You can also use the String.GetHashCode function from the Mono
library to retrieve a string's hash code. For more information, visit
http://msdn.microsoft.com/en-us/library/system.string.
gethashcode%28v=vs.110%29.aspx .
Sometimes, however, you don't want to compare for equality. Your intention might
be to determine which string takes more priority alphabetically, that is, whether
one string would appear before the other if they were both listed alphabetically in
a dictionary. You can achieve this using the String.Compare function. However,
again, be sure to use a version that features a StringComparison type in the
arguments, as shown in the following code sample 6-14. With this version, -1
would be returned if Str1 comes before Str2 , 1 would be returned if Str2
comes before Str1 , and 0 would be returned if the two strings are equal:
//Sort comparison
public int StringOrder (string Str1, string Str2)
{
//Ignores case
return string.Compare(Str1, Str2,
System.StringComparison.CurrentCultureIgnoreCase);
}
 
Search WWH ::




Custom Search