
- VBForums
- Visual Basic
- Visual Basic .NET
- [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
-
Jan 21st, 2007, 07:04 PM
#1
Thread Starter
Addicted Member
[2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
i want to convert the accents to regular characters
e.g.
"À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" --> "a"
i am currently using arrays and loops to replace them but it takes quite some time... it's not performance friendly at all.....
-
Jan 21st, 2007, 07:52 PM
#2
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
-
Jan 21st, 2007, 08:53 PM
#3
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
I am not sure if there should be such a method in the framework at all. À,Á, etc are not A and stripping them from accents destroys the whole concept of multisymbolic support.
Still, sometimes it can be necessary (for password input or searches with or without accents). One automated method would be to save the string to disk explicitly in ASCII and immediately reopen it but it'd be pretty awkward. You get performance problems not because of the analysis but because you are modifying a string many times - that is the bottleneck. Instead of using string.replace, use a stringbuilder. It is a lot faster when modifying characters in a row. You will always have to define a matrix of characters to be tested and replaced. Use either Ifs, Select Case or some other fancy source for that matrix.
VB Code:
Dim originalString As String = "bbbÀgggÁerdfÂfsdÃfeÄ,Å,à,á,â,ã,ä,å"
Dim CurrentChar As Char
Dim myBuilder As New System.Text.StringBuilder
Dim i As Integer
'loop through all letters in the string and replace them if needed
For i = 0 To originalString.Length - 1
CurrentChar = originalString(i)
Select Case CurrentChar
'make cases for all the characters you want stripped
Case "À"c
myBuilder.Append("a")
Case "Á"c
myBuilder.Append("a")
Case "Â"c
myBuilder.Append("a")
'etc
'etc
'etc
Case Else
'if the char is not in the black list, append it unchanged
myBuilder.Append(CurrentChar)
End Select
Next
'assign the contents of the stringBuilder to a string if you wish
Dim StrippedString As String
StrippedString = myBuilder.ToString
-
Jan 22nd, 2007, 01:10 AM
#4
Thread Starter
Addicted Member
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
i need this to allow users to search for names with accents without having to take care of the accents... thanks for the code will try it..hope it's faster than my method
-
Jan 22nd, 2007, 05:26 AM
#5
Thread Starter
Addicted Member
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
thanks u opened my eyes! i adapted your example with something i used for a asp vbcript project to detect unwanted characters....
here's how my function looks now..it's working ok and a lot faster than my previous method
VB Code:
Function encode(ByVal str As String)
Dim CurrentChar As Char
Dim myBuilder As New System.Text.StringBuilder
Dim i As Integer
Dim astr = "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å"
Dim estr = "È,É,Ê,Ë,è,é,ê,ë"
Dim ustr = "Ù,Ú,Û,Ü,ù,ú,û,ü"
Dim ostr = "Ò,Ó,Ô,ò,ó,ô,õ,ö,ð,Õ,Ö"
Dim ystr = "ý,ÿ,Ý"
Dim istr = "Ì,Í,Î,Ï,ì,í,î,ï"
Dim aestr = "Æ,æ"
Dim nstr = "Ñ,ñ"
'loop through all letters in the string and replace them if needed
For i = 0 To str.Length - 1
CurrentChar = str(i)
If InStr(astr, CurrentChar) > 0 Then
myBuilder.Append("a")
ElseIf InStr(estr, CurrentChar) > 0 Then
myBuilder.Append("e")
ElseIf InStr(istr, CurrentChar) > 0 Then
myBuilder.Append("i")
ElseIf InStr(ostr, CurrentChar) > 0 Then
myBuilder.Append("o")
ElseIf InStr(ustr, CurrentChar) > 0 Then
myBuilder.Append("u")
ElseIf InStr(ystr, CurrentChar) > 0 Then
myBuilder.Append("y")
ElseIf InStr(nstr, CurrentChar) > 0 Then
myBuilder.Append("n")
ElseIf InStr(aestr, CurrentChar) > 0 Then
myBuilder.Append("ae")
Else
myBuilder.Append(CurrentChar)
End If
Next
'assign the contents of the stringBuilder to a string if you wish
encode = myBuilder.ToString
End Function
thx again!
-
Jan 22nd, 2007, 05:49 AM
#6
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
it seems you are a vb6 programmer from your coding. I wont use InStr visual basic method but .Net's string contains method to do it.
VB Code:
Function encode(ByVal str As String)
Dim myBuilder As New System.Text.StringBuilder
Dim astr As String = "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å"
Dim estr As String = "È,É,Ê,Ë,è,é,ê,ë"
Dim ustr As String = "Ù,Ú,Û,Ü,ù,ú,û,ü"
Dim ostr As String = "Ò,Ó,Ô,ò,ó,ô,õ,ö,ð,Õ,Ö"
Dim ystr As String = "ý,ÿ,Ý"
Dim istr As String = "Ì,Í,Î,Ï,ì,í,î,ï"
Dim aestr As String = "Æ,æ"
Dim nstr As String = "Ñ,ñ"
'loop through all letters in the string and replace them if needed
For Each ch As Char In str
Select Case True
Case astr.Contains(ch)
myBuilder.Append("a")
Case estr.Contains(ch)
myBuilder.Append("e")
'...
'...
Case Else
myBuilder.Append(ch)
End Select
Next
'assign the contents of the stringBuilder to a string if you wish
encode = myBuilder.ToString
End Function
-
Jan 22nd, 2007, 06:02 AM
#7
Thread Starter
Addicted Member
Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
lol yep i am former vb6 user lol... still using old vb6 habits lol thanks for the tip

- VBForums
- Visual Basic
- Visual Basic .NET
- [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|