Finding a new line position in a VB.NET string variable using InStr

I have an application in VB.NET that gets string data from a database. This line contains data that looks like this:

"This update:
I have a problem with the application"

I need only the part of the data that comes after the new line, that is, "I have a problem with the application."

To do this, I am trying to find a position using InStr , where the row contains data in a new row. I tried many options, but they do not work.

I used "vbCrLf", Chr (13), "\ r \ n", "\ n", " <br/>", Environment.NewLine, but none of them work.

How can I get the data I need?

+3
source share
4 answers

A newline character can be represented either by a newline character ( Chr(10)) or by carriage / line return pairs ( Chr(13) + Chr(10)). This, of course, may vary depending on the data source. One way to achieve this is to make the string divided into these two characters with the ability to delete blank elements, throw the first one out and join the others with the new characters in between:

ReadOnly separators As Char() = New Char() {Chr(10), Chr(13)}
Private Function StripFirstLine(ByVal input As String) As String
    Dim parts() As String = input.Split(separators, StringSplitOptions.RemoveEmptyEntries)

    If parts.Length > 1 Then
        Return String.Join(Environment.NewLine, parts, 1, parts.Length - 1)
    Else
        Return input
    End If

End Function
+1
source

Use vbCrLf, not "vbCrLf"

It may also be that you have only linear channels or only curry returns, so using Chr (10) and Chr (13) might also be a better option.

+1
source

, , ... "ControlChars", "ControlChars.CrLf".

ControlChars @MSDN

+1

script :

    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText("d:\a.txt")
    TextBox2.Text = fileContents
    Dim a As Integer = 1

    For i = 1 To fileContents.Length
        Dim xx As String = Mid(fileContents, i, 1)
        If xx = Chr(10) Then
            If Mid(fileContents, i - 1, 1) = Chr(13) Then
                ListBox1.Items.Add(Mid(fileContents, a, (i - a) - 1))
                a = i
            End If
        End If
    Next
+1

Source: https://habr.com/ru/post/1714600/


All Articles