Remove '\' char from string c#

后端 未结 10 1019
谎友^
谎友^ 2020-12-15 03:16

I have the following code

string line = \"\"; 

while ((line = stringReader.ReadLine()) != null)
{
    // split the lines
    for (int c = 0; c < line.Len         


        
相关标签:
10条回答
  • 2020-12-15 03:27

    You can use String.Replace which basically removes all occurrences

    line.Replace(@"\", ""); 
    
    0 讨论(0)
  • 2020-12-15 03:27

    Try to replace

    string result = line.Replace("\\","");
    
    0 讨论(0)
  • 2020-12-15 03:27
             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);
                 }
             }
    
    0 讨论(0)
  • 2020-12-15 03:36

    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);
    
    0 讨论(0)
  • 2020-12-15 03:39

    You could use:

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

    or

    line.Replace(@"\", string.Empty);
    
    0 讨论(0)
  • 2020-12-15 03:41

    Why not simply this?

    resultString = Regex.Replace(subjectString, @"\\", "");
    
    0 讨论(0)
提交回复
热议问题