greener
greener

Reputation: 5069

VB function that strips accents from a string

I'm trying to have a function which takes a string and returns the same string without any accented letters. Instead, the accented letters should return the same letter without the accent. This function is not working:

function StripAccents(str)

accent   = "ÈÉÊËÛÙÏÎÀÂÔÖÇèéêëûùïîàâôöç"
noaccent = "EEEEUUIIAAOOCeeeeuuiiaaooc"

currentChar = ""
result = ""
k = 0
o = 0

FOR k = 1 TO len(str)
    currentChar = mid(str,k, 1)
    o = InStr(accent, currentChar)
    IF o > 0 THEN
        result = result & mid(noaccent,k,1)
    ELSE
        result = result & currentChar
    END IF
NEXT

StripAccents = result

End function

testStr = "Test : à é À É ç"
response.write(StripAccents(testStr))

This is the result using the above:

Test : E E Eu EE E

Upvotes: 0

Views: 4885

Answers (3)

Phil Allen
Phil Allen

Reputation: 171

I tried the example code with the correction added

Then I added more characters

Giving:

accent   = "àèìòùÀÈÌÒÙäëïöüÄËÏÖÜâêîôûÂÊÎÔÛáéíóúÁÉÍÓÚðÐýÝãñõÃÑÕšŠžŽçÇåÅøØ"
noaccent = "aeiouAEIOUaeiouAEIOUaeiouAEIOUaeiouAEIOUdDyYanoANOsSzZcCaAoO"

Now I realised that there are a few more to deal with, namely

æ
Æ
ß

These need converting first using a simple replace them with ae AE and ss

Then it works fine other than it is important to not have <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> or similar in the code

However having meta charset="UTF-8" in the header is not a big issue, it converts fine.

So if the code is needed on the page with <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%> in it, I do not know any answer to that

Thanks for the code greener, very useful for dealing with the common diacriticals :-)

Upvotes: 2

someprogrammer
someprogrammer

Reputation: 269

You should probably do a decomposition normalization first (NFD). I think you could do this in VBA using a call to the WinAPI function NormalizeString (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319093(v=vs.85).aspx). Then, you could remove the accent code points.

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Disregarding possible encoding problems - you must change

result = result & mid(noaccent,k,1)

to

result = result & mid(noaccent,o,1)

Upvotes: 2

Related Questions