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?
You can also try:
string res = string.Join("", sample.Split(Environment.NewLine.ToCharArray())
Environment.NewLine should make it independent of platform.
Environment.NewLine Property
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.
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)
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.
How about using a Regex?
var result = Regex.Replace(input, "\r\n", String.Empty)
If you just want to remove the new line at the very end use this
var result = Regex.Replace(input, "\r\n$", String.Empty)
In VB.NET there's a vbCrLf constant for linebreaks:
Dim s As String = "your string".Replace(vbCrLf, "")
Assign your string to a variable and then replace the line break and carriage return characters with nothing, like this:
myString = myString.Replace(vbCrLf, "")
I just had the same issue in my code today and tried which worked like a charm.
.Replace("\r\n")
Source: Stackoverflow.com