Want to remove double quotes from strings

I saw here a few questions related to removing double quotes. But that did not solve my problem. They said to use the Replace or Trim functions. I used, but the problem remains.

My code is:

DataTable dt = TypeConveriontHelper.ConvretExcelToDataTable(SourceAppFilePath); string targetPath = BatchFolderPath + @"\" + "TRANSACT.INX"; StreamWriter wrtr = null; wrtr = new StreamWriter(targetPath); for (int x = 0; x < dt.Rows.Count; x++) { string rowString = ""; for (int y = 0; y < dt.Columns.Count; y++) { if (y == dt.Columns.Count - 1) { rowString += "\"" + dt.Rows[x][y].ToString() + "\""; } else { rowString += "\"" + dt.Rows[x][y].ToString() + "\"~"; } } rowString.Replace('"', ' ').Trim(); wrtr.WriteLine(rowString); } wrtr.Close(); wrtr.Dispose(); 

I tried using rowString.Replace('"', ' ').Trim(); in the code above. But the double quotes are present as they are. Prove me the solution. Thanks.

+8
string c # replace
source share
7 answers

You need to return it to rowString :

 rowString = rowString.Replace('"', ' ').Trim(); 

The strings are immutable .

row.String.Replace(...) will return a row to you, since you are not assigning it anything that will be discarded. It will not change the original rowString .

You can use String.Empty or "" to replace double quotes with an empty string instead of a simple space. ' ' Therefore, your statement should be:

 rowString = rowString.Replace("\"", string.Empty).Trim(); 

(Remember to pass the double quote as the string "\"" , since overloading the method with string.Empty will require both parameters to be of type string).

You can get rid of Trim() at the end if you tried to remove the addition of spaces during string.Replace at the beginning or end of the line.

+22
source share

Trim can remove any character (s), not just spaces.

 myString = myString.Trim('"'); 

http://msdn.microsoft.com/en-us/library/d4tt83f9%28v=vs.110%29.aspx

+15
source share

This line contains an error.

 rowString.Replace('"', ' ').Trim(); 

Since both replace and trim returns a string, you need to assign this variable. Without assigning a string, you lose the value generated by the replace and trim methods.

 var myString = rowString.Replace('"', ' ').Trim(); //Or rowString = rowString.Replace('"', ' ').Trim(); 
0
source share

In VB, you should try the following:

 Replace("""", ""); 

But in C # you should try the following:

 Replace("\"", ""); 
0
source share

rowString.Replace('"', ' ') creates a new string object and does not modify the existing string.so object, you will have to assign the newly created string object to another variable or overwrite the existing variable.

 rowString=rowString.Replace('"', ' ').Trim() or var somestring=rowString.Replace('"', ' ').Trim() 
0
source share

As Habib mentioned, the lines are immutable. Any string manipulations should be returned using managed data such as Replace, Reverse, etc.

So, try assigning a replaced string to get the desired behavior.

0
source share

Your problem is a common mistake. Here is what you need to do:

 rowString = rowString.Replace('"', ' ').Trim(); 

Be sure to return the result to a variable ( rowString = ). If you don't, C # will still invoke methods, but it will not change this variable. This applies to any immutable objects .

Example:

 rowString.Replace('"', ' ').Trim(); 

will not modify the actual rowString .

 rowString = rowString.Replace('"', ' ').Trim(); 

replaces the old rowString value rowString result of the code on the right.

I wrote this answer and only realized that Habib explained the same thing above. :)

0
source share

All Articles