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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Reomve "\" 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
 
In your call to Replace, double up the backslash character.

Like C & C++, C# uses the backslash character to escape newlines, carriage-returns, tabs, etc., so to actually get one, you have to double it up.

From the C# language specification:
MSDN said:
A character that follows a backslash character (\) in a regular-string-literal-character must be one of the following characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs.

Chip H.




If you want to get the best response to a question, please check out FAQ222-2244 first
 
You don't seem to be getting the answers you want.

How about rephrasing the question?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
chiph is right and I don't see a reason for you to remove that chars. The following string doesn't contains the backslah character!
string t10 = "xftag Source=\"L\">500671";
If you display it using this line, you will see the right string:
MessageBox.Show(t10);
If you like to store it anywhere then in the destination you will be stored the following chars : xftag Source="L">500671
-obislavu-
 
I agree when you display it , it doesnt show "\" but when I try to update into DB using below its giving error
Original Msg contains string which has "\" wherever double quotes are there .
-

string strCmdText = "UPDATE system_exceptions SET OriginalMessage = '" + strOriginalMsg + "' WHERE ExcpMsgID="+myChildDataSet.Tables[0].Rows[0]["ExcpMsgID"].ToString();
OdbcCommand updDBCommand = new OdbcCommand(strCmdText,myConnection2);
updDBCommand.ExecuteNonQuery();
 
That's a different question that what you asked.

The question should be: "Why do double-quotes cause problems when writing to the database".

And the answer is that the double-quote is a reserved character in SQL. You should use ADO Parameters to pass string values to your database (but sadly, most people are too lazy to use them). The other way would be to use the .Replace method to double-up the quote characters:
Code:
  MyStringVar = MyStringVar.Replace("""", """""");
For a good reason to use parameter objects, do a Google on "SQL Injection".

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