How to eliminate ALL line breaks in string?

后端 未结 12 1775
傲寒
傲寒 2021-01-30 10:22

I have a need to get rid of all line breaks that appear in my strings (coming from db). I do it using code below:

value.Replace(\"\\r\\n\", \"\").Replace(\"\\n\"         


        
相关标签:
12条回答
  • 2021-01-30 11:13

    If you've a string say "theString" then use the method Replace and give it the arguments shown below:

    theString = theString.Replace(System.Environment.NewLine, "");

    0 讨论(0)
  • 2021-01-30 11:14

    Props to Yossarian on this one, I think he's right. Replace all whitespace with a single space:

    data = Regex.Replace(data, @"\s+", " ");
    
    0 讨论(0)
  • 2021-01-30 11:15

    According to wikipedia, there are numerous line terminators you may need to handle (including this one you mention).

    LF: Line Feed, U+000A
    VT: Vertical Tab, U+000B
    FF: Form Feed, U+000C
    CR: Carriage Return, U+000D
    CR+LF: CR (U+000D) followed by LF (U+000A)
    NEL: Next Line, U+0085
    LS: Line Separator, U+2028
    PS: Paragraph Separator, U+2029

    0 讨论(0)
  • 2021-01-30 11:16

    This is my first attempt at this, but I think this will do what you want....

    var controlChars = from c in value.ToCharArray() where Char.IsControl(c) select c;
    foreach (char c in controlChars)  
       value = value.Replace(c.ToString(), "");
    

    Also, see this link for details on other methods you can use: Char Methods

    0 讨论(0)
  • 2021-01-30 11:17

    I'd recommend removing ALL the whitespace (char.IsWhitespace), and replacing it with single space.. IsWhiteSpace takes care of all weird unicode whitespaces.

    0 讨论(0)
  • 2021-01-30 11:19

    Assuming that 8232 is unicode, you can do this:

    value.Replace("\u2028", string.Empty);
    
    0 讨论(0)
提交回复
热议问题