50

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?

10 Answers 10

130

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't notice the VB.NET tag, but as that is in there +1.
Good, but how about tabs. how one can remove tabs in the string?
13

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.

2 Comments

Basically, I am using the ckeditor which inserts carriage returns in the html it generates. This is causing me problems later on for various reasons, and I don't know if I can change a setting in ckeditor to stop generating the carriage returns. Also, it's probably safer to remove the carriage returns at the serverside stage right?
it's working. .Replace("\r\n", string.Empty).Replace("\n", string.Empty)
5

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

Dim s As String = "your string".Replace(vbCrLf, "")

3 Comments

Have you tried this? It won't work with the example given. I suspect that's because vbCrLf is actually defined as vbCr + vbLf, which isn't present in the string, but I'm not sure. Splitting them up is the only solution that will remove the line returns from the string; see my answer for a sample.
For completeness: For "the normal" dotnet framework there is Environment.Newline that might be CRLF. Also as @CodyGray said: it is 2 chars.
This works for for removing line breaks in labels for Visual Studio 2013.
4

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.

3 Comments

As an observation, this will work but involves 2 arrays and 2x n intermediate strings; but they should all be eligible for gen-0 collection, so still pretty cheap
Good point, I may eventually need to remove the spaces too. Thanks for pointing this out.
This is the best way (I think) to remove hard \n and \r characters in a string.
2

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

 myString = myString.Replace(vbCrLf, "")

1 Comment

Have you tried this? It won't work with the example given. I suspect that's because vbCrLf is actually defined as vbCr + vbLf, which isn't present in the string, but I'm not sure. Splitting them up is the only solution that will remove the line returns from the string; see my answer for a sample.
2

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

Comments

1

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)

Comments

0

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)

Comments

0

Microsoft helped me on this one. I have a TextBox that I enter info into, when I hit Enter it transfers that info to another TextBox. The first TextBox has a CrLf which interfered with it's look. With this e.Handled, no CrLf. Hope it helps. I failed to say, VB.NET and WinForms. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress?view=windowsdesktop-6.0

Private nonNumberEntered As Boolean = False
Private Sub TxtBxGetString_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TxtBxGetString.KeyPress

    TxtBxReversed.Text = ""

    If Asc(e.KeyChar) = 13 Then
        If nonNumberEntered = False Then
            e.Handled = True
        End If
        str = TxtBxGetString.Text
        TxtBxReversed.Text = str
        TxtBxLength.Text = Len(str)
        TxtBxLength.Focus()
    End If
End Sub

Comments

-1

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

.Replace("\r\n")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.