45

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.

PVitt
  • 11,500
  • 5
  • 51
  • 85
maffo
  • 1,393
  • 4
  • 18
  • 35
  • 1
    `Trim(new char[] {'\\'})` will remove all \ characters from the start or the end. It 'trims' them off. As @user978511 states, you can use `Replace("\\", "")`. (FYI, his use of the @ character means "take this string literally, without applying escaping rules") – JohnL Dec 05 '11 at 09:51
  • None of the solutions below works for me... – ccoutinho Feb 08 '16 at 16:23
  • Regex.Unescape() – Alexander Jun 25 '17 at 18:14

10 Answers10

133

You could use:

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

or

line.Replace(@"\", string.Empty);
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • 8
    That's because Replace does not change the string itself, it returns the changed string. So you have to do like my answer, and write `line = line.Rep...` – Øyvind Bråthen Dec 05 '11 at 09:52
  • 1
    These are not slashes. These are escape characters. They are used here to escape quotes. Consider using single quote instead of double quotes. You will not need slashes there then. – Andrey Marchuk Dec 05 '11 at 09:52
  • 1
    @PoiXen - In the string you have posted, `\"` is an escape sequence that lets `"` appear in the string. There are no back slash characters in it. – Oded Dec 05 '11 at 09:53
  • For an optimized solution: http://stackoverflow.com/questions/1120198/most-efficient-way-to-remove-special-characters-from-string – Gerhard Powell Mar 11 '13 at 16:27
  • does not work for this "\"something://player.vimeo.com/video/27735162\"" – reza.cse08 Mar 03 '19 at 05:51
  • 1
    @reza.cse08 Because there's no \ in you string, you have quote, which is escaped with \ - like \" – Andrey Marchuk Mar 06 '19 at 10:19
8

You can use String.Replace which basically removes all occurrences

line.Replace(@"\", ""); 
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
7

to remove all '\' from a string, simply do the following:

myString = myString.Replace("\\", "");
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
7
line = line.Replace("\\", "");
craig1231
  • 3,769
  • 4
  • 31
  • 34
6

Why not simply this?

resultString = Regex.Replace(subjectString, @"\\", "");
FailedDev
  • 26,680
  • 9
  • 53
  • 73
5

Try to replace

string result = line.Replace("\\","");
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
4

I have faced this issue so many times and I was surprised that many of these don't work.

I simply deserialize the string with Newtonsoft.Json and I get cleartext.

string rough = "\"call 12\"";
rough = JsonConvert.DeserializeObject<string>(rough);

//the result is: "call 12";
Olorunfemi Davis
  • 1,001
  • 10
  • 23
4

Try using

String sOld = ...;
String sNew =     sOld.Replace("\\", String.Empty);
Shai
  • 25,159
  • 9
  • 44
  • 67
2

Trim only removes characters at the beginning and the end of the string, that's why your code doesn't quite work. You should use Replace instead:

line.Replace(@"\", string.Empty);
Falanwe
  • 4,636
  • 22
  • 37
2
         while ((line = stringReader.ReadLine()) != null)
         {
             // split the lines
             for (int c = 0; c < line.Length; c++)
             {
                 line = line.Replace("\\", "");
                 lineBreakOne = line.Substring(1, c - 2);
                 lineBreakTwo = line.Substring(c + 2, line.Length - 2);
             }
         }
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73