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

3
  • 1
    Does this answer your question? How do I write a backslash (\‌) in a string? Commented Apr 5, 2023 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). Commented Apr 5, 2023 at 14:10
  • If you indeed have characters \a (alert) and \b (backspace), 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. Commented Apr 5, 2023 at 14:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.