[Gambas-user] An utility component for Gambas, Accented characters

Lewis Balentine lewis at ...3412...
Tue Dec 2 00:28:56 CET 2014


UGH, another stupid mistake on my part.
Well that having been corrected I offer the following:

'------------------------
' Gambas module file

' The problem I have as an ignorant, narrow minded, English speaking
' American is that I do not know of many accented characters .... :>(
Private Const AccentInFilter As String = "ŷŶũŨẽẼĩĨòåš"
Private Const AccentOutFilter As String = "yYuUeEiIoas"

Public Sub Main()
   Dim S As String
   S = "This is a test: ŷŶũŨẽẼĩĨòåš"
   Print S
   S = StripAccentCharacters(S)
   Print S
End

Public Function StripAccentCharacters(InputString As String) As String
   Return StringFilter(InputString, AccentInFilter, AccentOutFilter)
End

Private Function StringFilter(InputString As String, InFilter As String, 
OutFilter As String) As String
   ' This would be much more efficent in "C" with single byte ASCII 
characters
   ' but with UTF-8 there are 1,2,3 and 4 byte characters. Thus we need 
a higher
   ' level functions to deal with variable size characters anyway.
   Dim P, I As Integer
   Dim C As String
   Dim R As String = ""

   If InputString = "" Then Return R
   For I = 1 To String.Len(InputString)
     ' get the next character
     C = String.Mid(InputString, I, 1)
     ' try to locate the character in the input filter string
     P = String.InStr(InFilter, C)
     ' if found then
     If P > 0 Then
       ' use the character from the output filter string
       R = R & Mid(OutFilter, P, 1)
     Else
       ' otherwise the character from the input string
       R = R & C
     Endif
   Next
   Return R
End
'------------------------------------------------------

This is a test: ŷŶũŨẽẼĩĨòåš
This is a test: yYuUeEiIoas
'------------------------------------------------------

The advantage to this approach is that that different Filter Strings and 
Functions can be quickly constructed as needed for the desired 
modifications. You could also use it to build a "one-timepad" encryption 
algorithm (which is where I extracted it from --- some of my old VB6 code).


On 12/01/2014 04:42 PM, Charlie Reinl wrote:
> Am Montag, den 01.12.2014, 16:27 -0600 schrieb Lewis Balentine:
>>>> ...and read the documentation of InStr carefully again.<<
>> "This function only deal with ASCII strings. To manipulate UTF-8
>> strings, use the String class."
>>
>> P = String.InStr("Ũ", "ŷŶũŨẽẼĩĨòåš", gb.Binary)
>> P = String.InStr("Ũ", "ŷŶũŨẽẼĩĨòåš", gb.IgnoreCase)
>> P = String.InStr("Ũ", "ŷŶũŨẽẼĩĨòåš")
>> P = String.InStr("Ũ", "xxxxŷŶũŨẽẼĩĨòåšXXXX", gb.Binary)
>> P = String.InStr("Ũ", "xxxxŷŶũŨẽẼĩĨòåšXXXX", gb.IgnoreCase)
>> P = String.InStr("Ũ", "xxxxŷŶũŨẽẼĩĨòåšXXXX")
>>
>> same result ... P=0
>>
> Salut Lewis,
>
> Benoît said : and read the documentation of InStr carefully again.
> Me too re-read http://gambaswiki.org/wiki/comp/gb/string/instr ,
> (inverse your arguments).
>
> InStr(String As String, Pattern As String )
>




More information about the User mailing list