Remove carriage return from string
I would like to insert the following into a string
<p>some text here</p>
<p>some text here</p>
<p>some text here</p>
I want it to go into a string as follows
<p>some text here</p><p>some text here</p><p>some text here</p>
i.e. without the carriage returns.
How do I achieve this?
Solution 1:
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.
Solution 2:
How about:
string s = orig.Replace("\n","").Replace("\r","");
which should handle the common line-endings.
Alternatively, if you have that string hard-coded or are assembling it at runtime - just don't add the newlines in the first place.
Solution 3:
If you want to remove spaces at the beginning/end of a line too(common when shortening html) you can try:
string.Join("",input.Split('\n','\r').Select(s=>s.Trim()))
Else use the simple Replace
Marc suggested.
Solution 4:
In VB.NET there's a vbCrLf constant for linebreaks:
Dim s As String = "your string".Replace(vbCrLf, "")