I try to write my own version of the string, and I didn’t understand yet how to check the equality of such strings (inherited from IEnumerable<char>
) case-insensitively.
Of course, this code:
<code>if (_size != other._size)
return false;
for (var i = 0; i < _size && i < other._size; i++)
if (char.ToUpper(this[i]) != char.ToUpper(other[i]))
return false;
return true;
</code>
<code>if (_size != other._size)
return false;
for (var i = 0; i < _size && i < other._size; i++)
if (char.ToUpper(this[i]) != char.ToUpper(other[i]))
return false;
return true;
</code>
if (_size != other._size)
return false;
for (var i = 0; i < _size && i < other._size; i++)
if (char.ToUpper(this[i]) != char.ToUpper(other[i]))
return false;
return true;
is not optimal. (The _size
variable means length, but is a private variable that can be changed inside the class.) This code calls char.ToUpper()
two times, whereas I just need to compare that chars. What is a better way?
Note: check must work for all writing systems like Latin, Cyrillic, Arabic etc.
2