Hi,
How can I detect new line from string. I tried Environment.NewLine but its not working. Initially I tried to replace it directly but it was not working after I tried to detect new line. Then I came to know that its not detecting new line.

As temporary solution I have taken a richbox with new line and comparing it. Its working fine but its not the proper method so please tell me how to do it properly.

Dani AI

Generated

You are running into a line-ending mismatch. Environment.NewLine (and vbCrLf/vbNewLine on Windows) is CRLF (Chr(13) + Chr(10)), but many sources emit only LF (Chr(10)), e.g., pasted text, Unix-origin files, or certain controls. That explains why your compare failed until you switched to vbCrLf in the case where the text actually contained CRLF. As noted, your source/control may be producing only LF, while ’s example works when the data truly contains CRLF.

If the goal is to remove newlines before inserting into a list, detect and strip all common variants rather than relying on a single constant.

' Detect: returns >= 0 if any CR or LF is present
If input.IndexOfAny({ControlChars.Cr, ControlChars.Lf}) >= 0 Then
    ' newline(s) found
End If
' Remove all newline styles (CRLF, CR, or LF)
Dim noNewlines As String =
    System.Text.RegularExpressions.Regex.Replace(input, "\r\n|\r|\n", "")
' If you prefer to keep word boundaries, replace with a space instead of ""
' Dim normalized As String = Regex.Replace(input, "\r\n|\r|\n", " ")

If you actually want to split lines into items, normalize/split in one step and avoid ArrayList in favor of a typed list:

Dim items As List(Of String) =
    New List(Of String)(
        input.Split(New String() {vbCrLf, vbCr, vbLf},
                    StringSplitOptions.RemoveEmptyEntries))

Notes:

  • Text coming from different controls or platforms can mix endings, so normalize once, then process.
  • vbCr, vbLf, and vbCrLf are fine when you explicitly want those sequences, but for robustness a regex targeting all variants is simpler to maintain.

Recommended Answers

All 3 Replies

The richtextbox uses chr(10) (VBLf) to designate a new line. So if that is working, then check for VBLf (this is a VB character constant).

On non-Unix systems, the System.Environment.NewLine is a two character string equivalent to chr(13) & chr(10). You could also use the VB constant VBCrLf for this.

What is it that you are trying to accomplish? There are most likely other methods that could be suggested.

The text file with this post has one line of text and a newline. You can use either VBCRLF or VBNEWLINE

 Dim txt As String = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "\newLine.txt")

 If txt.Contains(vbNewLine) Then
            MsgBox(txt.IndexOf(vbNewLine))
        End If

I was trying to remove new lines from string before inserting it to array list. I tried chr(13) and chr(10) its not working, but VBCrLf is working. thnx

it works ;) thnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.