Home > Mobile >  How to get the Indexof backslash(\) in C#?
How to get the Indexof backslash(\) in C#?

Time:09-02

is there way to find out the indexof backslash in string variable?

i have string

var str = "      \"AAP, FB, VOD, ART, BAG, CAT, DDL\"\n    "

int stIdx = str.indexof('\"') // output as 6

int edIdx = str.indexof('\', stIdx 1); // output as -1

output i'm looking for is as below

AAP, FB, VOD, ART, BAG, CAT, DDL

CodePudding user response:

You need to escape your backslash to use it as a char value.

Cause backslash is a special character, '\' didn't refer to backslash as a character, but as an instruction which let you escape any character.

int index = str.IndexOf('\\'); //should give you the right answer

CodePudding user response:

The character sequence '\n' is used for a newline.

string str = "Hi\n";

In the above example str consists of the characters 'H', 'i', and a newline character. There is no '\' character.

Try the following:

int edIdx = str.indexof('\n', stIdx 1);
  • Related