0

...databox.text (from example code below) contains a large list of combined words(domain names) previously populated in the program. There is 1 per each line. In this example, it initially looks like:

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

The following code below saves the text inside databox to tlistfiltered.txt and then searches tlistfiltered.txt to retrieve all lines that contain any of the items in the list "arr()", and then populates listview(lv) with the results. This works just fine, but the results look like:

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

but what I need is the "found string" (from arr()list to be Proper case so the result would be:

thePeople.com
trueHistory.com
neverChange.com

Here is the code....

 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

Is there a way to do this on the fly? I have tried StrConv etc, but it will only convert the entire line to proper case. I only want the "found" word contained within the line to be converted....

edit:

after seeing @soohoonigan 's answer, i edited

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

                               databox.AppendText(match)
        Next
    Next

to this:

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

and got the desired result!

t4nk
  • 3
  • 4

2 Answers2

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

End Class

soohoonigan
  • 2,342
  • 2
  • 10
  • 18
0

I'm not sure to understand the need for using files for intermediate steps and deleting them at the end for example.

  • First step: getting the lines of the input
    That can be done by using the Lines property of databox (which I suspect to be a TextBox or RichTextBox ; if it's not the case we can still use a Split on the Text property)

    Dim lines = databox.Lines ' or databox.Text.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
    
  • Second step: we want to filter those lines to keep only the ones containing the searched texts
    For this there are several way, a simple one would be to use a Linq query to get the job done
  • Third step: transforming the result of the filter replacing the searched text by it's capitalized form
    So we continue the started query and add a projection (or mapping) to do the transformation.
    We need to use TextInfo.ToTitleCase for that.

    ' 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
    
Sehnsucht
  • 5,019
  • 17
  • 27
  • i see what you're saying, my code is very inefficient... ill take your response and try implementing that way... – t4nk Aug 05 '16 at 16:02