Results 1 to 7 of 7

Thread: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    [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.....

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: [2005] replace all accents in a string (e.g. "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å" to "a")

    U can use
    String.Replace

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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:
    1. Dim originalString As String = "bbbÀgggÁerdfÂfsdÃfeÄ,Å,à,á,â,ã,ä,å"
    2.         Dim CurrentChar As Char
    3.  
    4.         Dim myBuilder As New System.Text.StringBuilder
    5.  
    6.         Dim i As Integer
    7.         'loop through all letters in the string and replace them if needed
    8.         For i = 0 To originalString.Length - 1
    9.  
    10.             CurrentChar = originalString(i)
    11.             Select Case CurrentChar
    12.                 'make cases for all the characters you want stripped
    13.                 Case "À"c
    14.                     myBuilder.Append("a")
    15.                 Case "Á"c
    16.                     myBuilder.Append("a")
    17.                 Case "Â"c
    18.                     myBuilder.Append("a")
    19.  
    20.                     'etc
    21.                     'etc
    22.                     'etc
    23.  
    24.                 Case Else
    25.                     'if the char is not in the black list, append it unchanged
    26.                     myBuilder.Append(CurrentChar)
    27.             End Select
    28.         Next
    29.  
    30.         'assign the contents of the stringBuilder to a string if you wish
    31.         Dim StrippedString As String
    32.         StrippedString = myBuilder.ToString
    VB 2005, Win Xp Pro sp2

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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:
    1. Function encode(ByVal str As String)
    2.         Dim CurrentChar As Char
    3.         Dim myBuilder As New System.Text.StringBuilder
    4.  
    5.         Dim i As Integer
    6.         Dim astr = "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å"
    7.         Dim estr = "È,É,Ê,Ë,è,é,ê,ë"
    8.         Dim ustr = "Ù,Ú,Û,Ü,ù,ú,û,ü"
    9.         Dim ostr = "Ò,Ó,Ô,ò,ó,ô,õ,ö,ð,Õ,Ö"
    10.         Dim ystr = "ý,ÿ,Ý"
    11.         Dim istr = "Ì,Í,Î,Ï,ì,í,î,ï"
    12.         Dim aestr = "Æ,æ"
    13.         Dim nstr = "Ñ,ñ"
    14.  
    15.  
    16.         'loop through all letters in the string and replace them if needed
    17.         For i = 0 To str.Length - 1
    18.  
    19.             CurrentChar = str(i)
    20.             If InStr(astr, CurrentChar) > 0 Then
    21.                 myBuilder.Append("a")
    22.             ElseIf InStr(estr, CurrentChar) > 0 Then
    23.                 myBuilder.Append("e")
    24.             ElseIf InStr(istr, CurrentChar) > 0 Then
    25.                 myBuilder.Append("i")
    26.             ElseIf InStr(ostr, CurrentChar) > 0 Then
    27.                 myBuilder.Append("o")
    28.             ElseIf InStr(ustr, CurrentChar) > 0 Then
    29.                 myBuilder.Append("u")
    30.             ElseIf InStr(ystr, CurrentChar) > 0 Then
    31.                 myBuilder.Append("y")
    32.             ElseIf InStr(nstr, CurrentChar) > 0 Then
    33.                 myBuilder.Append("n")
    34.             ElseIf InStr(aestr, CurrentChar) > 0 Then
    35.                 myBuilder.Append("ae")
    36.             Else
    37.                 myBuilder.Append(CurrentChar)
    38.             End If
    39.         Next
    40.  
    41.         'assign the contents of the stringBuilder to a string if you wish
    42.         encode = myBuilder.ToString
    43.     End Function

    thx again!

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Function encode(ByVal str As String)
    2.         Dim myBuilder As New System.Text.StringBuilder
    3.  
    4.         Dim astr As String = "À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å"
    5.         Dim estr As String = "È,É,Ê,Ë,è,é,ê,ë"
    6.         Dim ustr As String = "Ù,Ú,Û,Ü,ù,ú,û,ü"
    7.         Dim ostr As String = "Ò,Ó,Ô,ò,ó,ô,õ,ö,ð,Õ,Ö"
    8.         Dim ystr As String = "ý,ÿ,Ý"
    9.         Dim istr As String = "Ì,Í,Î,Ï,ì,í,î,ï"
    10.         Dim aestr As String = "Æ,æ"
    11.         Dim nstr As String = "Ñ,ñ"
    12.  
    13.  
    14.         'loop through all letters in the string and replace them if needed
    15.         For Each ch As Char In str
    16.             Select Case True
    17.                 Case astr.Contains(ch)
    18.                     myBuilder.Append("a")
    19.                 Case estr.Contains(ch)
    20.                     myBuilder.Append("e")
    21.                     '...
    22.                     '...
    23.                 Case Else
    24.                     myBuilder.Append(ch)
    25.             End Select
    26.         Next
    27.  
    28.         'assign the contents of the stringBuilder to a string if you wish
    29.         encode = myBuilder.ToString
    30.     End Function

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width