Remove carriage return from string

前端 未结 9 2045
逝去的感伤
逝去的感伤 2021-01-03 17:41

I would like to insert the following into a string

some text here

some text here

some text here

相关标签:
9条回答
  • 2021-01-03 17:55

    Assign your string to a variable and then replace the line break and carriage return characters with nothing, like this:

     myString = myString.Replace(vbCrLf, "")
    
    0 讨论(0)
  • 2021-01-03 18:01

    For VB.net

    vbcrlf = environment.newline...

    Dim MyString As String = "This is a Test" & Environment.NewLine & " This is the second line!"

    Dim MyNewString As String = MyString.Replace(Environment.NewLine,String.Empty)
    
    0 讨论(0)
  • 2021-01-03 18:04

    In VB.NET there's a vbCrLf constant for linebreaks:

    Dim s As String = "your string".Replace(vbCrLf, "")
    
    0 讨论(0)
  • 2021-01-03 18:07

    Since you're using VB.NET, you'll need the following code:

    Dim newString As String = origString.Replace(vbCr, "").Replace(vbLf, "")
    

    You could use escape characters (\r and \n) in C#, but these won't work in VB.NET. You have to use the equivalent constants (vbCr and vbLf) instead.

    0 讨论(0)
  • 2021-01-03 18:07

    You can also try:

    string res = string.Join("", sample.Split(Environment.NewLine.ToCharArray())
    

    Environment.NewLine should make it independent of platform.

    Recommended Read:

    Environment.NewLine Property

    0 讨论(0)
  • 2021-01-03 18:10

    I just had the same issue in my code today and tried which worked like a charm.

    .Replace("\r\n")
    
    0 讨论(0)
提交回复
热议问题