Reputation: 928
How can I properly use substring, at least, make it work ?
I want to find the position of this substring '#fff'\">
in the stringString this.style.backgroundColor='#fff'\">Choose a translation mode,
I'm doing this :
string substrToSearch = "'#fff'\\\">";
int substrPosition = stringString.IndexOf(substrToSearch);
Unfortunately, substrPosition = -1, and it is not working. May someone help me ?
EDIT SOLVED :
I don't realy understand why I can't do this string quote = " " ";
but I can do this quote = " \" "
,in fact I know that I can but.. here, the "\" is used as an escaping character, so why in the substring that I'm searching it is not interpreted as an escape character but as simple text ?
To me , string substrToSearch = "'#fff'\">";
, substrToSearch is '#fff'"> as \"
= "
.
To be simple, why \"
is not interpreted as an escape character ?
Upvotes: 2
Views: 196
Reputation: 101730
The answer to your added question is fundamental to the concept of string escaping. The syntax string quote = " " ";
is problematic because a compiler would think that the string ended with the second quote and not the third one. String escaping allows representing an actual quotation mark with \"
, so the actual value of " \" "
is "
, not \"
.
Upvotes: 1
Reputation: 3445
Here you go...
[TestMethod]
public void StringIndex
{
var stringString = "this.style.backgroundColor='#fff'\">Choose a translation mode,:";
var substrToSearch = "'#fff'\">";
var substrPosition = stringString.IndexOf(substrToSearch);
}
Upvotes: 1
Reputation: 131
try this:
string substrToSearch = "'#fff'\">";
the stringString is not ecapsed when you access it
Upvotes: 1