0

textbox我有一个循环,它逐行提取字符串,每次迭代都有多个条件。如果条件为真,我想从中删除该行textbox有没有办法从字符串中删除整行?这是我的代码..

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

当找到关键字“BY”时,是否可以复制下一行字符串?我需要帮助这是我的论文项目..

4

3 回答 3

1

试试这个,tTextBox

    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()
于 2013-02-25T15:10:25.040 回答
1

您正在使用许多旧的 VB6 函数,虽然 VB.NET 仍然支持这些函数以实现向后兼容性,但实际上并不推荐用于新开发。最好使用 .NET 框架的特性,这些特性使此类任务变得更加容易。例如:

  • 使用List(Of String)orStringBuilder代替Dim strSet(1000) As String
  • 使用MessageBox.Show代替MsgBox
  • 使用String.SubString代替Mid
  • 使用String.Length代替Len
  • 使用ContrlCharsorEnvironment.NewLine代替Chr(10)andChr(13)
  • 最重要的是,请不要使用GoTo

但是,您缺少的最重要的工具是该String.Split方法,该方法将一个字符串拆分为一个或多个分隔符上的字符串数组。在这种情况下,您可以在换行字符串上拆分字符串。例如:

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()

或者,您可以使用StringReader该类,这取决于您的偏好和风格,可能更容易遵循:

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()

或者,如果你想证明你有多聪明,你可以使用 LINQ 扩展方法在一行中完成整个事情:

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

但是,如果逻辑是拼写出来的,而不是像那样全部挤在一起,有时代码更容易阅读:)

更新

这是一种获取所有“by”行以及紧随其后的行的方法:

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()
于 2013-02-25T15:26:47.030 回答
0

我不会详细介绍,但通过检测“\n”,我认为您应该能够完成这项工作。你试过吗?

于 2013-02-25T14:50:36.577 回答