0

I have this piece of code:

var index = some_string.IndexOf(Delimiter);

When Delimiter equals "\u0003", this seems not to work, so I have solved this as follows:

var index = some_string.IndexOf(Delimiter, System.StringComparison.Ordinal);

Delimiter equals "\u0003" and I can confirm this is working (I did some trial-and-error with all possible System.StringComparison enums and the Ordinal (and its case-related brother) were able to find the delimiter).

It's the idea that this piece of code also works for other delimiter characters. Some obvious ones, like "|", "@" are already tested and are working fine. I don't expect my customers to use language-typical characters.

Can anybody confirm if ±all to-be-expected-as-delimiter characters are covered by the Ordinal enum, or expressed more clearly: which non-language-typical characters are not found by the expression var index = some_string.IndexOf(Delimiter, System.StringComparison.Ordinal);?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 2
    Why are you afraid that it would not work for any characters? If you use `Ordinal` you are on the safe side (as [opposed to `InvariantCulture`](https://stackoverflow.com/questions/17573147/special-symbols-in-net-and-indexof), for example). – Tim Schmelter Oct 14 '22 at 08:54
  • 1
    `StringComparison.Ordinal` is a binary comparison. Maybe it makes sense to wonder about sort order, but why would that matter for `IndexOf`? – BurnsBA Oct 14 '22 at 14:52
  • @BurnsBA: my question was caused by the fact that a simple `IndexOf()` seemed not to work, so I found out that adding the `Ordinal` parameter made it work. That was for the `"\u0003"` character. So now that I have a solution for that one character, I would like to be sure for all possible delimiter characters. My question is not about sorting, it's purely about the `IndexOf()` method. I've edited my question accordingly. – Dominique Oct 15 '22 at 09:27
  • 1
    Per [this answer](https://stackoverflow.com/a/2584204/1462295), IndexOf goes to https://github.com/dotnet/coreclr/blob/775003a4c72f0acc37eab84628fcef541533ba4e/src/utilcode/newapis.cpp#L775 and `ordinal` will return naive search algorithm (`FastIndexOfString` line 577). That *should* answer your question, but there's a culture issue I can't pin down. Your code (without compare options) above works for me but fails in dotnetfiddle, regardless of culture. So I can't say more than that. – BurnsBA Oct 15 '22 at 15:00

0 Answers0