My problem is that String.IndexOf returns -1
. I would expect it to return 0
.
The parameters:
text
= C:\\Users\\User\\Desktop\\Sync\\̼
(note the Combining Seagull Below character)
stringToTrim
= C:\\Users\\User\\Desktop\\Sync\\
When I check for the index, using int index = text.IndexOf(stringToTrim);
, the value of index
is -1
. I found that using an ordinal string comparison solved this problem of mine:
int index = text.IndexOf(stringToTrim, StringComparison.Ordinal);
Reading online, a lot of Unicode characters (like U+00B5 and U+03BC) map to the same symbol, so it would be a good idea to expand on this and normalize both strings:
int index = text.Normalize(NormalizationForm.FormKD).IndexOf(stringToTrim.Normalize(NormalizationForm.FormKD), StringComparison.Ordinal);
Is this the correct approach to check at what index one string contains all sequential characters of another string? So the idea is, you normalize when you want to check that symbols are a match, but you don't normalize when you want to check characters by their encoded values (allow duplicate symbols, therefore)? Also, could someone please explain why int index = text.IndexOf(stringToTrim);
did not find a match at the start of the string? In other words, what is it actually doing under the covers? I would have expected it to start searching characters from the beginning of the string to the end of the string.
"C:\\Users\\User\\Desktop\\Sync\\̼".IndexOf("C:\\Users\\User\\Desktop\\Sync\\");
Make sure to copy this text entirely/exactly from here! – Schoolfellow