【问题标题】:visual basic search text for string, display results with propercase字符串的visual basic搜索文本,以正确的形式显示结果
【发布时间】:2016-08-05 15:13:43
【问题描述】:

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

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

以下代码将数据框内的文本保存到 tlistfiltered.txt,然后搜索 tlistfiltered.txt 以检索包含列表“arr()”中任何项目的所有行,然后使用结果。这工作得很好,但结果看起来像:

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

但我需要的是“找到的字符串”(来自 arr()list 是正确的,所以结果是:

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

得到了想要的结果!

【问题讨论】:

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


    【解决方案1】:

    公开课表1

    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
    

    结束类

    【讨论】:

      【解决方案2】:

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

      • 第一步:获取输入的行
        这可以通过使用数据框的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
        

      【讨论】:

      • 我明白你在说什么,我的代码效率很低......我会接受你的回应并尝试以这种方式实现......
      猜你喜欢
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多