如何从textbox vb.net中删除字符串

时间:2013-02-25 14:44:36

标签: vb.net string

我有一个循环,它逐行地从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"也可以复制下一行字符串。找到了吗?我需要帮助这是我的论文项目..

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

  • 使用List(Of String)StringBuilder代替Dim strSet(1000) As String
  • 使用MessageBox.Show代替MsgBox
  • 使用String.SubString代替Mid
  • 使用String.Length代替Len
  • 使用ContrlCharsEnvironment.NewLine代替Chr(10)Chr(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()