t4nk t4nk - 4 months ago 13
Vb.net Question

visual basic search text for string, display results with propercase

...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!

Answer

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