字符串的可视基本搜索文本,使用正确的

时间:2016-08-05 15:13:44

标签: vb.net visual-studio-2015 strconv

... databox.text(来自下面的示例代码)包含先前在程序中填充的组合单词(域名)的大列表。每行1个。在这个例子中,它最初看起来像:

thepeople.com
truehistory.com
workhorse.com
whatever.com
neverchange.com
...

下面的代码将databox中的文本保存到tlistfiltered.txt,然后搜索tlistfiltered.txt以检索包含列表“arr()”中任何项目的所有行,然后使用以下代码填充listview(lv)结果。这很好用,但结果如下:

thepeople.com
truehistory.com
neverchange.com
...

但我需要的是“找到的字符串”(来自arr()列表是正确的情况所以结果将是:

thePeople.com
trueHistory.com
neverChange.com

这是代码......

 Dim s As String = databox.Text
        File.WriteAllText(dloc & "tlistfiltered.txt", s)
        databox.Clear()

        Dim text2() As String = System.IO.File.ReadAllLines(dloc & "tlistfiltered.txt")

        Dim arr() As String = {"people", "history", "change"}
        For index1 = 0 To arr.GetUpperBound(0)

            Dim YesLines() As String = Array.FindAll(text2, Function(str As String)

                                                                Return str.Contains(arr(index1))

                                                            End Function).ToArray

            databox.Visible = True
            For index2 = 0 To YesLines.GetUpperBound(0)
                Dim match As String = (YesLines(index2)) & vbCrLf

                                   databox.AppendText(match)
            Next
        Next
        s = databox.Text
        File.WriteAllText(dloc & "tlistfilteredfinal.txt", s)
        databox.Clear()
        domains = (From line In File.ReadAllLines(dloc & "tlistfilteredfinal.txt") Select New ListViewItem(line.Split)).ToArray
        lv.Items.Clear()
        My.Computer.FileSystem.DeleteFile(dloc & "tlistfiltered.txt")
        My.Computer.FileSystem.DeleteFile(dloc & "tlistfilteredfinal.txt")
        BackgroundWorker1.RunWorkerAsync()
    End Sub

有没有办法在飞行中这样做?我已经尝试过StrConv等,但它只会将整行转换为正确的情况。我只希望转换行中包含的“找到”单词....

编辑:

看到@soohoonigan的回答后,我编辑了

      databox.Visible = True
        For index2 = 0 To YesLines.GetUpperBound(0)
            Dim match As String = (YesLines(index2)) & vbCrLf

                               databox.AppendText(match)
        Next
    Next

到此:

databox.Visible = True
            For index2 = 0 To YesLines.GetUpperBound(0)
                Dim match As String = (YesLines(index2)) & vbCrLf
                Dim myTI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
                If match.Contains(arr(index1)) Then
                    match = match.Replace(arr(index1), myTI.ToTitleCase(arr(index1)))
                    'StrConv(match, vbProperCase)
                    databox.AppendText(match)
                End If
            Next

并获得了理想的结果!

2 个答案:

答案 0 :(得分:0)

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim test As String = "thepeople.com"
    Dim search As String = "people"
    Dim myTI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US", False).TextInfo
    If test.Contains(search) Then
        test = test.Replace(search, myTI.ToTitleCase(search))
        MsgBox(test)
    End If
    Me.Close()
End Sub

结束班

答案 1 :(得分:0)

我不确定是否需要将文件用于中间步骤并在最后删除它们。例如。

  • 第一步:获取输入的行 这可以通过使用数据库的Lines属性来完成(我怀疑它是TextBoxRichTextBox;如果不是这样,我们仍然可以在Text属性上使用Split

    Dim lines = databox.Lines ' or databox.Text.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    
  • 第二步:我们想要过滤那些行,只保留包含搜索文本的那些行 为此,有几种方法,一种简单的方法是使用Linq查询来完成工作
  • 第三步:转换过滤器的结果,用它的大写形式替换搜索到的文本 因此,我们继续启动查询并添加投影(或映射)以进行转换 我们需要使用TextInfo.ToTitleCase

    ' See soohoonigan answer if you need a different culture than the current one
    Dim textInfo = System.Globalization.CultureInfo.CurrentCulture.TextInfo
    
    Dim query = From word in arr
                From line in lines
                Where line.Contains(word)
                Let transformed = line.Replace(word, textInfo.ToTitleCase(word))
                select transformed
    ' We could omit the Let and do the Replace directly in the Select Clause