0

I am strugling with getting the index of sequence "\a\b" in string. Any idea how to do it? I am doing following:

string expr = "Hello\a\b";
Console.WriteLine(expr.IndexOf("\a\b"));

This code returns 0 instead of expected 5

  • 1
    Does this answer your question? [How do I write a backslash (\‌) in a string?](https://stackoverflow.com/questions/18532691/how-do-i-write-a-backslash-in-a-string) – GSerg Apr 05 '23 at 11:41
  • No, that is different. If I understand it correctly writing IndexOf(\\a\\b) will search for character \. However I am looking for \a and \b which are different character. Please correct me if I am wrong. The output for my case is correct only when using IndexOf("\a\b", StringComparison.Ordinal). – Matouš Kovář Apr 05 '23 at 14:10
  • If you indeed have characters ``\a`` ([alert](https://www.compart.com/en/unicode/U+0007)) and ``\b`` ([backspace](https://www.compart.com/en/unicode/U+0008)), then yes, that duplicate is wrong for you and the accepted answer is correct. However, not only these characters are rather unusual in a string, you are also explicitly saying in the title of the question that you want to search for the backslash character, for which that duplicate is correct. – GSerg Apr 05 '23 at 14:50

1 Answers1

2
string expr = "Hello\a\b";
var inx = expr.IndexOf("\a\b", StringComparison.Ordinal);

Refer to documentation https://learn.microsoft.com/en-us/dotnet/api/System.StringComparison?view=net-6.0

Cyril ANDRE
  • 124
  • 9