How to replace escaped strings?

B

Brett

I've been trying different methods for replacing escaped strings. None
work. I even have a custom Replace() method but still can't get what I
need. Some of the characters I need to replace are:
\r\n with nothing
\" with only single quote.

myString = myString.Replace(@"\"", @""");

Replace(myString, @"\"", @""");
gives this error:
No overload for method 'Replace' takes '2' arguments

or
Replace(myString, @"\\""", @""""));
which doesn't work


//Replace function that I'm using
public static String Replace(String strText,String strFind,String
strReplace)
{
int iPos=strText.IndexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strText.Substring(0,iPos) + strReplace;
strText=strText.Substring(iPos+strFind.Length);
iPos=strText.IndexOf(strFind);
}
if(strText.Length>0)
strReturn+=strText;
return strReturn;
}

Any suggestions?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett said:
I've been trying different methods for replacing escaped strings. None
work. I even have a custom Replace() method but still can't get what I
need. Some of the characters I need to replace are:
\r\n with nothing
\" with only single quote.

myString = myString.Replace(@"\"", @""");

You don't want those to be verbatim string literals, due to the quote

myString = myString.Replace ("\\\"", "\"");
 
B

Brett

Brett said:
That isn't working. The before and after strings are the same. Here's an
example of the original string:

<A href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">

I want to eliminate the \ in the above.

Thanks,
Brett

A little additional info.

string mytest = "<A
href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">";
string _mytest = mytest.Replace("\\\"", "--");

When I walk through this code to the closing method brace, here's what I get
in the watch window:

_mytest error: identifier '_mytest' out of scope
mytest.Replace("\\\"", "--") error: 'mytest.Replace' does not exist
mytest "<A href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">"
string

No errors. Any idea why that is going on?
 
B

Bill Butler

<A href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">

I want to eliminate the \ in the above.

assuming you mean something like this
string str = "<A
href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">";

There IS NO '\' in the string

The \" that you see in the string declaration is stored as an " in the
actual string
if you do
Console.WriteLine("{0}", str);
you get
<A href="http://dslstart.verizon.net/vzn.dsl/dog/advance.htm">

Hopefully this is what you mean

Bill
 
B

Brett

J

Jon Skeet [C# MVP]

Brett said:
A little additional info.

string mytest = "<A
href=\"http://dslstart.verizon.net/vzn.dsl/dog/advance.htm\">";
string _mytest = mytest.Replace("\\\"", "--");

There's no backslash in the string above. There is in the source code,
but not in the actual string.
When I walk through this code to the closing method brace, here's what I get
in the watch window:

That's not showing you the actual string - it's escaping it for you,
somewhat unhelpfullly.

See http://www.pobox.com/~skeet/csharp/strings.html#debugger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top