Highlight search string with special characters

noccy

Newcomer
Joined
Jan 17, 2005
Messages
13
Hi!

I am trying to highlight the search query in a gridview like this:

Visual Basic:
'foo is the gridview
'strQuery is the search string

        
        Dim itm As Object
        Dim cel As TableCell
        Dim iHit As Integer
        Dim strOriginal As String
        Dim strStart As String
       
        If Len(strQuery) > 0 Then
        
            For Each itm In foo.Rows
                      
                For Each cel In itm.Cells
                    
                                   
                    If cel.Text.ToLower.Contains(strQuery.ToLower) And cel.Text <> " " Then
                        
                                              
                        strStart = ""
                        strOriginal = cel.Text
                    
                        iHit = InStr(strOriginal.ToLower, strQuery.ToLower)
                    
                                           
                        If iHit > 1 Then
                                                
                            strStart = Left(strOriginal, iHit - 1)
                        
                        End If
                        
                        cel.Text = strStart & "<span style=color:red;background-color:Lime;font-weight:bold>" & Mid(strOriginal, iHit, Len(strQuery)) & "</span>"
                          
                          cel.Text = cel.Text & Right(strOriginal, Len(strOriginal) - (iHit + Len(strQuery)) + 1)                                     
                     
                    End If
                    
                Next
           
                Next
        
        End If

This works like charm, but if the search string contains any special charachters like the norwegian æ, ø and å, it wont work....

Any ideas?


noccy
 
special char to lower

What do the special chars equal when converted to lower? Meaning, what is the toupper and the tolower of the æ, ø and å?

Just a guess but since it works w/o those characters, that's probably where the error's at, and it never finds it's tolower version in the table, so you're going to have to find a way to handle those.

I haven't messed with this so there's probably a better way, but I'm pretty sure this would work:
Look at each char in the querystring:
if it's a standard, convert.tolower then append to a stringbuilder
if it's not standard, just append to the stringbuilder.
When you've finished converting the query into the stringbuilder, then use the stringbuilder.tostring to compare to your cells.
 
If you are using .Net then try the string methods for comparisons etc. rather than the VB style ones.
The .Net 2 methods allow you to specify the compare options you need i.e.
Visual Basic:
  ihit = strOriginal.IndexOf(strQuery, StringComparison.CurrentCultureIgnoreCase)
this will prevent errors where upper and lower case cannot simply be converted and should be more reliable.
 
Yes, it seems to be a problem with upper and lower... "Å" ToLower returns "Å"... I will try your suggestions as soon as I am back at work...

Thanks :)
 
Back
Top