» » » How to remove accent marks and german umlauts from URLs and Strings in .NET

How to remove accent marks and german umlauts from URLs and Strings in .NET

I wrote two functions in .NET to remove easly any accent and umlaut marks from a string and to get the equivalent non-accented string, this is very useful to normalize URLs; for example, with the StripAccentMarks() method, from “àbcdèéfghìlmnòpqrstùvzäöüÄÖÜ” u’ll get “abcdeefghilmnopqrstuvzaouAOU”;

besides, if u’ll need to convert each german umlaut to the corresponding two-letter chars, u can also use the ReplaceGermanUmlauts() method…

Public Shared Function StripAccentMarks(ByVal input_string As String) As String
   Dim n_s As String = input_string.Normalize(NormalizationForm.FormD)
   Dim s As New StringBuilder()
   Dim c As Char
   For i As Integer = 0 To n_s.Length - 1
      c = n_s(i)
      If System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) <> System.Globalization.UnicodeCategory.NonSpacingMark Then
         s.Append(c)
      End If
   Next
   Return s.ToString
End Function
Public Shared Function ReplaceGermanUmlauts(ByVal input_string As String) As String
   Dim s As String = input_string</pre>
s = s.Replace("ä", "ae")
 s = s.Replace("ö", "oe")
 s = s.Replace("ü", "ue")
 s = s.Replace("Ä", "Ae")
 s = s.Replace("Ö", "Oe")
 s = s.Replace("Ü", "Ue")
 s = s.Replace("ß", "ss")
 Return s
End Function

Have a nice day!

Max 😀

Leave a Reply