Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove "\" from a String

Status
Not open for further replies.

DilipKS

Programmer
Mar 8, 2004
30
CH
Hi

string t10 = "xftag Source=\"L\">500671";
I want to remove "\" from above string and resultant string shld be "xftag Source="L">500671"

I have tried many ways but I am not getting the desired output . One of the ways I tried , pls see below

string t10 = "xftag Source=\"L\">500671";
string r10 = t10.Replace("\"", "*");
r10 = r10.Replace('*','"');

Still I am getting same output i.e. "xftag Source=\"L\">500671"
 
try

string r10 = t10.Replace("\\", "");

Rob


Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Hi Rob
Thnks for reply
I have tried this already ..but its not working .
I debugged it and can see the output ..string it still same.

string r10 = t10.Replace("\\", "");

Have u run this code ?
 
I'm not sure what your trying to do here? Consider the following code
Code:
string x = "foo =\"foo\"";
string y = x.Replace("\\", "");
Response.Write(x + "<br />");
Response.Write(y);

The backslashes in the string, x, are escape characters telling the compiler that the quotes do not end the string. Hence if you run this code the ouput would be...

foo="foo"
foo="foo"

Therefore is you wanted to replace the \ there is no need as it is an escape character anyway.

Now consider the following
Code:
string x = "foo =\\foo\\";
string y = x.Replace("\\", "");
Response.Write(x + "<br />");
Response.Write(y);
Here the backslashes escpae the following backslash. the output is...

foo=\foofoo=foo

This help any?

Remember you can use the @ preprocessor directive to automatically escape a string like this..
Code:
string x = @"foo=\foo\";
string y = @"foo=""foo""";
Response.Write(x + "<br />");
Response.Write(y);

In this case all characters are escaped by default and quotes are used to escape quotes as you would in VB. The output is

foo=\foofoo="foo"

Any help?

Rob


Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Just a guess, but your replace function may be seeing the double quotes as an end of string. Try replacing the double quotes inside, then try replacing the slashes. Then put the quotes back in if you need them. It might work...
 
>>>> Therefore is you wanted to replace the \ there is no need as it is an escape character anyway.

I need to replace it because am not printing it or manipulating it..I need to insert this string to Database, its actually a big XML, just for an example i used that string.
If I insert this to Database "xftag Source=\"L\">500671" , I get error and more over I need actual string "xftag Source="L">500671" to database not the "\" one.
Does it makes clear Rob ...?
 
Hmmm, you shouldnt be getting a problme here as the string never contains the backslashes. They are just used to escape the double quotes when the string is literally defined and shouldn't be in the string passed to the database. You could try assigning it to another string beforehand to see if that works...

How is the XML formed? Where does it come from? Where are the backslashes added to the string? Can you put the XML into a System.Xml object and read the string back from that?

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
1) I have a XML msg in a field called OrigMsg in a table in Sybase DB. The xml MSg is a continuous string with XML tags.
2) Reading this field from DB and writing it into an XML i.e.
StreamWriter sw = new StreamWriter("C:\\Temp\\Test.xml", false );
sw.Write(strOriginalMsg);sw.Close();
where the strOriginalMsg is populated from DB.
3) Reading same XML and displaying in the grid using Dataset.
4) editing and saving back to Dataset and converting this Dataset to XML again.
So when it saves back to XML it appaends "\" where ever it finds double quotes as in attributes.
I readthis msg reading frm XML file using

StreamReader sw = new StreamReader("C:\\Temp\\TestFlp.xml");
strOriginalMsg = sw.ReadToEnd();
so this strOriginalMsg has "\" , do you suggest any other method which If i use to read XML, it ignores "\" ???
Is it clear ?

 
Not really %-) :)

Bit confused as I can't see where in this process you are getting the backslashes added in. I've never experienced this when converting a DataSet to XML using GetXml() method or WriteXml() or when reading an XML file into a StreamReader....

Exactly which process is adding the backslashes?

You shoudln't really need the temporary file at all though. You can create the dataset directly from the XML string using ReadXml() and then GetXml to convert back to a string.

Rob



Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
DilipKS said:
I need to replace it because am not printing it or manipulating it..I need to insert this string to Database,

Try using ADO.NET parameter objects. They will allow you to write your quote-marks to the database.

Also, how are you reading the XML? The one thing you shouldn't be doing is using a TextReader. You need to use a reader specific to XML, or loading it into a DomDocument.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top