Guest User

Removing diacritics in Silverlight (String.Normalize issue)

a guest
Feb 23rd, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. Public Function RemoveDiacritics(ByVal searchInString As String) As String
  2. Dim returnValue As String = ""
  3.  
  4. Dim formD As String = searchInString.Normalize(System.Text.NormalizationForm.FormD)
  5. Dim unicodeCategory As System.Globalization.UnicodeCategory = Nothing
  6. Dim stringBuilder As New System.Text.StringBuilder()
  7.  
  8.  
  9. For formScan As Integer = 0 To formD.Length - 1
  10. unicodeCategory = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(formD(formScan))
  11. If unicodeCategory <> System.Globalization.UnicodeCategory.NonSpacingMark Then
  12. stringBuilder.Append(formD(formScan))
  13. End If
  14. Next
  15.  
  16. returnValue = stringBuilder.ToString().Normalize(System.Text.NormalizationForm.FormC)
  17.  
  18. Return returnValue
  19.  
  20. End Function
  21.  
  22. public string Normalize ()
  23. {
  24. return Normalization.Normalize (this, 0);
  25. }
  26.  
  27. public string Normalize (NormalizationForm normalizationForm)
  28. {
  29. switch (normalizationForm)
  30. {
  31. default:
  32. return Normalization.Normalize (this, 0);
  33. case NormalizationForm.FormD:
  34. return Normalization.Normalize (this, 1);
  35. case NormalizationForm.FormKC:
  36. return Normalization.Normalize (this, 2);
  37. case NormalizationForm.FormKD:
  38. return Normalization.Normalize (this, 3);
  39. }
  40. }
  41.  
  42. <System.Runtime.CompilerServices.Extension()> _
  43. Public Function RemoveDiacritics(ByVal searchInString As String) As String
  44. Dim returnValue As String = ""
  45.  
  46. returnValue = searchInString
  47.  
  48. returnValue = returnValue.ReplaceLowerAndUpperCase("À", "A")
  49. returnValue = returnValue.ReplaceLowerAndUpperCase("Á", "A")
  50. returnValue = returnValue.ReplaceLowerAndUpperCase("Â", "A")
  51. returnValue = returnValue.ReplaceLowerAndUpperCase("Ã", "A")
  52. returnValue = returnValue.ReplaceLowerAndUpperCase("Ä", "A")
  53. returnValue = returnValue.ReplaceLowerAndUpperCase("Å", "A")
  54. returnValue = returnValue.ReplaceLowerAndUpperCase("Æ", "A")
  55.  
  56. returnValue = returnValue.ReplaceLowerAndUpperCase("Ç", "C")
  57.  
  58. returnValue = returnValue.ReplaceLowerAndUpperCase("È", "E")
  59. returnValue = returnValue.ReplaceLowerAndUpperCase("É", "E")
  60. returnValue = returnValue.ReplaceLowerAndUpperCase("Ê", "E")
  61. returnValue = returnValue.ReplaceLowerAndUpperCase("Ë", "E")
  62.  
  63. returnValue = returnValue.ReplaceLowerAndUpperCase("Ì", "I")
  64. returnValue = returnValue.ReplaceLowerAndUpperCase("Í", "I")
  65. returnValue = returnValue.ReplaceLowerAndUpperCase("Î", "I")
  66. returnValue = returnValue.ReplaceLowerAndUpperCase("Ï", "I")
  67.  
  68. returnValue = returnValue.ReplaceLowerAndUpperCase("Ñ", "N")
  69.  
  70. returnValue = returnValue.ReplaceLowerAndUpperCase("Ò", "O")
  71. returnValue = returnValue.ReplaceLowerAndUpperCase("Ó", "O")
  72. returnValue = returnValue.ReplaceLowerAndUpperCase("Ô", "O")
  73. returnValue = returnValue.ReplaceLowerAndUpperCase("Õ", "O")
  74. returnValue = returnValue.ReplaceLowerAndUpperCase("Ö", "O")
  75.  
  76. returnValue = returnValue.ReplaceLowerAndUpperCase("Ù", "U")
  77. returnValue = returnValue.ReplaceLowerAndUpperCase("Ú", "U")
  78. returnValue = returnValue.ReplaceLowerAndUpperCase("Û", "U")
  79. returnValue = returnValue.ReplaceLowerAndUpperCase("Ü", "U")
  80.  
  81. returnValue = returnValue.ReplaceLowerAndUpperCase("Ý", "Y")
  82.  
  83. returnValue = returnValue.ReplaceLowerAndUpperCase("Æ", "AE")
  84. returnValue = returnValue.ReplaceLowerAndUpperCase("Œ", "OE")
  85.  
  86. Return returnValue
  87.  
  88. End Function
  89.  
  90. <System.Runtime.CompilerServices.Extension()> _
  91. Public Function ReplaceLowerAndUpperCase(ByVal searchInString As String, ByVal oldString As String, ByVal newString As String) As String
  92. Dim returnValue As String = ""
  93.  
  94. returnValue = searchInString.Replace(oldString.ToLower, newString.ToLower)
  95. returnValue = returnValue.Replace(oldString.ToUpper, newString.ToUpper)
  96.  
  97. Return returnValue
  98. End Function
Advertisement
Add Comment
Please, Sign In to add comment