Replace accented characters?

Hi,
On OS X, when you hold certain keys down - the user can choose accented characters of that particular letter.
I need to be able to replace accented a and e characters, with the non accented a and e, if the user has entered them.

I thought of maybe replacing the accented character with the non accented character in the keydown event, as the user types - but there must be LOTS of accented versions for a and e. Therefore that way seems impractical.

Is this possible a different way, and if so - how?

Thank you all in advance.

dim key as string = "" Msgbox key.ConvertEncoding(Encodings.ASCII)

@Michel,
From your example, I get the idea that I can convert the entire text field to ascii this way - thus replacing all accented characters?

Try it out:

dim key as string = "asdlfaspoasa-sdsadas-dfkmasdfm" Msgbox key.ConvertEncoding(Encodings.ASCII)

Or use MBS plugin

RemoveAccentsMBS(text as string, IgnoreCase as boolean = false) as string

Thanks.
@Christian - is there anything your plugins CAN’T do :slight_smile: :slight_smile:

Every time I struggle with something, your plugins come to the rescue :slight_smile:

[quote=197375:@Richard Summers]@Michel,
From your example, I get the idea that I can convert the entire text field to ascii this way - thus replacing all accented characters?[/quote]

Correct. For instance in TextChange. Or whenever you need to get that data to process it.

Thanks Michel :slight_smile:

  • 1

Seems I should have put an entire alphabet as example, since apparently one letter did not suffice as answer in the first post to attempt to help, and you preferred the very same code with more letters. Next time, I’ll put an entire encyclopedia :confused:

There…

dim key as string = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ??????????????????????????????????????????????????????????????????????????????" Msgbox key.ConvertEncoding(Encodings.ASCII)

@Michel,
I thought that post was also from you - that’s why I selected that as the answer.
I have now corrected it, to display that indeed, YOU were the person who answered it first.

Je suis dsol (or should that be: Je suis desole) :slight_smile:

Thank you :slight_smile:

Note that this solution is incomplete and will fail for certain languages. Check for example:

dim key as string = "???" Msgbox key.ConvertEncoding(Encodings.ASCII)

It returns only question marks.

So, be aware of the fact that it works only for a very limited group of accented characters used in the Western Europe and use it ONLY if you’re 100% sure no one from, let’s say, Poland will run your application… :wink:

A much better solution is to use MBS:

dim key as string = "????????????????" Msgbox RemoveAccentsMBS(key, false)

Not only it deals well with Western European accents, it also recognises nearly all Polish ones, with a notable exception of "? (and “?”) at least in my version of MBS Plugins.

Yeah, converting characters might be tricky.

Just curious, Richard. Why do you want to do this?

RemoveAccentsMBS has a long replacement list for accented characters. I made it long ago for database queries to ignore accents.

As this is an OS X question here is a native solution:

[code]Module StringExtensions

Function Normalize(Extends s As String) As String

Declare Function CFStringCreateMutableCopy Lib "CoreFoundation" (CFAllocatorRef As Ptr, _
                 maxLength As Integer, theString As CFStringRef) As Ptr
Declare Function CFStringTransform Lib "CoreFoundation" (CFMutableStringRef As Ptr, CFRange As Ptr, _
                 transform As CFStringRef, reverse As Boolean) As Boolean
Declare Function UTF8String Lib "Foundation" Selector "UTF8String" (NSString As Ptr) As CString

Dim mutableStringRef As Ptr = CFStringCreateMutableCopy(Nil, 0, s)
If Not CFStringTransform(mutableStringRef, Nil, "Any-Latin; Latin-ASCII", False) Then
   Raise New RuntimeException()
End
Return UTF8String(mutableStringRef)

End Function

End Module[/code]
You use it like this:

[code]Const michel = “ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ” + _
“???” + _
“???·???”
Const grzegorz = “???ó???Ó???”

Dim txt As String = michel + grzegorz
MsgBox txt.Normalize()
[/code]
See CFStringTransform

Indeed. Anything beyond common Western European languages will trigger question marks. The encodings.ASCII solution is just a quick fix.

I have no doubt you put a lot of work into that, and that as usual, this is probably the best and fastest way.

Very nice.

Ok,
I have a text field called H1Field, and I need it to only accept the following characters:
A B C D E F

Therefore, if a user types in any accented version of A B C D E F - I need them replaced.

Here is what I have so far:

I have created a function called ReplaceAccented in my main module, containing this code:

[code]Declare Function CFStringCreateMutableCopy Lib “CoreFoundation” (CFAllocatorRef As Ptr, _
maxLength As Integer, theString As CFStringRef) As Ptr
Declare Function CFStringTransform Lib “CoreFoundation” (CFMutableStringRef As Ptr, CFRange As Ptr, _
transform As CFStringRef, reverse As Boolean) As Boolean
Declare Function UTF8String Lib “Foundation” Selector “UTF8String” (NSString As Ptr) As CString

Dim mutableStringRef As Ptr = CFStringCreateMutableCopy(Nil, 0, s)
If Not CFStringTransform(mutableStringRef, Nil, "Any-Latin; Latin-ASCII", False) Then
   Raise New RuntimeException()
End
Return UTF8String(mutableStringRef)[/code]

I then have this code (which needs to go in a text field’s change event), in order to replace any accented characters as they are typed:

Dim TextToReplace As String = "ÀÁÂÃÄÅÆÇÈÉÊËÐàáâãäåæçèéêë?????????????????????" TextToReplace.ReplaceAccented()

Would this code replace (for example) à with a, as soon as they type it?
I cannot test this as I am currently away from my main machine.

Thank you for all the help.