user2107624
user2107624

Reputation: 87

How remove line of string from textbox vb.net

I have a loop which extracts the string from the textbox line by line and multiple conditions for each iteration. if a condition is true i want to remove the line from the textbox is there a way to remove the entire line from the string? Here is my code..

Dim fileContents As String
    fileContents = txtOCR.Text
    Dim strSet(1000) As String
    Dim a As Integer = 1
    Dim words As String

    MsgBox(fileContents)

    For i = 1 To fileContents.Length
        Dim xx As String = Mid(fileContents, i, 1)

        'parse text line by line
        If xx = Chr(10) Then
            If Mid(fileContents, i - 1, 1) = Chr(13) Then
                strSet(i) = Mid(fileContents, a, (i - a) - 1)

    'count words
                Dim intCount As Integer
                intCount = 1
                For b = 1 To Len(strSet(i))
                    If Mid(strSet(i), b, 1) = " " Then
                        intCount = intCount + 1
                    End If
                Next

        If txtTitle.Text = "" And intCount = 1 Then
                txtTitle.Text = " " & strSet(i)

    ElseIf intCount = 1 Then
                    If strSet(i).Contains("BY") = True Then
                        GoTo lastline

                    ElseIf strSet(i).Contains("by") = True Then
                        GoTo lastline

                    ElseIf strSet(i).Contains("By") = True Then
                        GoTo lastline


                    Else
                        txtTitle.Text = txtTitle.Text + " " & strSet(i)

                    End If

            End If

            a = i

        End If
    Next

Also is it possible to copy the next line of string when the keyword "BY" is found? I need help this is for my thesis project..

Upvotes: 0

Views: 4554

Answers (3)

SysDragon
SysDragon

Reputation: 9888

Try this, t is the TextBox:

    Dim linesResult As New List(Of String)()
    Dim iNumWords As Integer
    Dim bFound As Boolean

    For iLine As Integer = 0 To t.Lines.Length - 1
        Dim words() As String = t.Lines(iLine).Split(" "c)

        If bFound Then TextBox2.Text &= t.Lines(iLine) & Environment.NewLine()

        iNumWords += words.Length
        bFound = False

        For Each elem As String In words
            If elem.Equals("by", StringComparison.CurrentCultureIgnoreCase) Then
                bFound = True
                Exit For
            End If
        Next

        If Not bFound Then linesResult.Add(t.Lines(iLine))
    Next
    t.Lines = linesResult.ToArray()

Upvotes: 1

Steven Doggart
Steven Doggart

Reputation: 43743

You are using a lot of old VB6 functions which, while still supported in VB.NET for backwards compatibility, are not really recommended for new development. It would be better to use the features of the .NET framework which make this kind of task far easier. For instance:

  • Use a List(Of String) or StringBuilder instead of Dim strSet(1000) As String
  • Use MessageBox.Show instead of MsgBox
  • Use String.SubString instead of Mid
  • Use String.Length instead of Len
  • Use ContrlChars or Environment.NewLine instead of Chr(10) and Chr(13)
  • And above all, please don't use GoTo

However, the most important tool you are missing is the String.Split method, which splits a string into an array of strings on one or more delimiters. In this case, you can split the string on the new-line string. For instance:

Dim builder As New StringBuilder()
For Each line As String In fileContents.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
    If Not line.ToLower().Contains("by") Then
        builder.AppendLine(line)
    End If
Next
txtOCR.Text = builder.ToString()

Or, you could use the StringReader class, which, depending upon your preferences and style, may be even easier to follow:

Dim builder As New StringBuilder()
Dim reader As New StringReader(txtOCR.Text)
While True
    Dim line As String = reader.ReadLine()
    If line Is Nothing Then Exit While
    If Not line.ToLower().Contains("by") Then
        builder.AppendLine(line)
    End If
End While
txtOCR.Text = builder.ToString()

Or, if you want to prove how clever you are, you can use a LINQ extension method to do the whole thing in one line:

txtOCR.Text = String.Join(Environment.NewLine, fileContents.Split(New String() {Environment.NewLine}, StringSplitOptions.None).Where(Function(x) Not x.ToLower().Contains("by")))

But, sometimes code is easier to read if the logic is spelled out rather than all crammed together like that :)

Update

Here's one way you could get all of the "by" lines with the line that immediately follows them:

Dim builder As New StringBuilder()
Dim reader As New StringReader(txtOCR.Text)
While True
    Dim line As String = reader.ReadLine()
    If line Is Nothing Then Exit While
    If line.ToLower().Contains("by") Then
        builder.AppendLine(line)
        builder.AppendLine(reader.ReadLine())
    End If
End While
txtOCR.Text = builder.ToString()

Upvotes: 1

user1722889
user1722889

Reputation:

I am going not into much details but by detecting "\n", I think you should be able to do this work. Have u tried it?

Upvotes: 0

Related Questions