Results 1 to 7 of 7

Thread: [RESOLVED] Search for Word(s) Ignoring Case. Then convert the words to ProperCase

  1. #1

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Resolved [RESOLVED] Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    Code:
     Public Sub HighLightStart(ByVal R As RichTextBox)
            Highlight(R, RoomNames, Color.Cyan)
            Highlight(R, ItemList, Color.Yellow)
            Highlight(R, ObjectList(RoomNumber), Color.Lime)
        End Sub
    
        Public Sub Highlight(ByVal r As RichTextBox, ByVal MyList As List(Of String), ByVal MyColor As Color)
            For Each strWord As String In MyList
                Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
                For Each wMatch As Match In wordMatchers.Matches(r.Text)
                    With r
                        .SelectionStart = wMatch.Index
                        .SelectionLength = wMatch.Value.Length
                        .SelectionColor = MyColor
                    End With
                Next
            Next
            r.Select(r.TextLength, 0)
            r.ScrollToCaret()
        End Sub
    The above searches for words in lists of string. But ignores stuff if the case doesn't exactly match that in the list.

    For example...
    Glass of Water (Is inside the list)
    Glass of water (Is not inside the list and is ignored if found in the RichTextBox)

    Ideally, I'd like it to find the words regardless of case and convert them to propercase (Glass Of Water) when found.

    As always any help will be greatly appreciated.

    Thanks
    The glass is half full if you're filling it, and half empty if you're emptying it.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,288

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    i'd use a dictionary and make sure when adding and looking up items to always use te 'all lower case' variante of the text as key.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    Code:
    Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord), RegexOptions.IgnoreCase)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    For converting to ProperCase, the method you were previously using was ok.
    The regex in my last post specifies IgnoreCase

  5. #5

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    Thanks Paul. As always your reply is top notch.

    ATM it is ignoring the case as you said it would. But it doesn't convert to ProperCase?

    Code:
                   With r
                        .SelectionStart = wMatch.Index
                        .SelectionLength = wMatch.Value.Length
                        .SelectionColor = MyColor
                        .SelectedText.ConvertToProperCase ??
                    End With
    The glass is half full if you're filling it, and half empty if you're emptying it.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    what was wrong with:

    .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)

    ? that's what you were using before...

  7. #7

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Search for Word(s) Ignoring Case. Then convert the words to ProperCase

    Ahh, I see. I didn't realize I had removed the line.

    It works now thank you very much.

    Managed to +rep you this time. Thanks a lot.
    The glass is half full if you're filling it, and half empty if you're emptying it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width