Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove '\' char from string c#

Tags:

c#

trim

I have the following code

string line = ""; 

while ((line = stringReader.ReadLine()) != null)
{
    // split the lines
    for (int c = 0; c < line.Length; c++)
    {
        if ( line[c] == ',' && line[c - 1] == '"' && line[c + 1] == '"')
        {
            line.Trim(new char[] {'\\'}); // <------
            lineBreakOne = line.Substring(1, c  - 2);
            lineBreakTwo = line.Substring(c + 2, line.Length - 2);
        }
    }
}

I have added a comment net to the line I am wondering about. I want to remove all '\' chars from the string. Is this the correct way to to this? I doesnt work. All \ are still in the string.

like image 711
maffo Avatar asked Dec 05 '11 09:12

maffo


People also ask

How do you remove a given character from string in C?

We have removeChar() method that will take a address of string array as an input and a character which you want to remove from a given string. Now in removeChar(char *str, char charToRemove) method our logic is written.

Can you remove a char from a string?

Using 'str.replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


2 Answers

You could use:

line.Replace(@"\", "");

or

line.Replace(@"\", string.Empty);
like image 193
Andrey Marchuk Avatar answered Sep 18 '22 10:09

Andrey Marchuk


You can use String.Replace which basically removes all occurrences

line.Replace(@"\", ""); 
like image 8
Sandeep Bansal Avatar answered Sep 20 '22 10:09

Sandeep Bansal