2009-02-09 37 views
3

在.NET中有一個返回根的信(不喜歡變音符號的特殊屬性的字母)的函數,還挺:有沒有函數返回特殊字符的根字母?

Select Case c 
    Case "á", "à", "ã", "â", "ä", "ª" : x = "a" 
    Case "é", "è", "ê", "ë" : x = "e" 
    Case "í", "ì", "î", "ï" : x = "i" 
    Case "ó", "ò", "õ", "ô", "ö", "º" : x = "o" 
    Case "ú", "ù", "û", "ü" : x = "u" 

    Case "Á", "À", "Ã", "Â", "Ä" : x = "A" 
    Case "É", "È", "Ê", "Ë" : x = "E" 
    Case "Í", "Ì", "Î", "Ï" : x = "I" 
    Case "Ó", "Ò", "Õ", "Ô", "Ö" : x = "O" 
    Case "Ú", "Ù", "Û", "Ü" : x = "U" 

    Case "ç" : x = "c" 
    Case "Ç" : x = "C" 

    Case Else 
     x = c 
End Select 

此代碼錯過了一些信件,但它僅適用於例如緣故:)

回答

2

順便說一句(完全無關的問題),你的代碼對字符串進行操作。這不僅效率低下,實際上並沒有什麼意義,因爲您對個字符而不是字符串感興趣,而這些是.NET中不同的數據類型。

要獲得一個單字符文字而不是字符串文字,追加c到您的文字:

Select Case c 
    Case "á"c, "à"c, "ã"c, "â"c, "ä"c, "ª"c : x = "a"c 
    ' … and so on. ' 
End Select 
1

從阿赫亞沙斯特里響應取,在這裏我給你的VB.NET代碼和C#一個複製從他的偉大的答案:)

VB:

Imports System.Text 
Imports System.Globalization 

''' <summary> 
''' Removes the special attributes of the letters passed in the word 
''' </summary> 
''' <param name="word">Word to be normalized</param> 
Function RemoveDiacritics(ByRef word As String) As String 
    Dim normalizedString As String = word.Normalize(NormalizationForm.FormD) 
    Dim r As StringBuilder = New StringBuilder() 
    Dim i As Integer 
    Dim c As Char 

    For i = 0 To i < normalizedString.Length 
     c = normalizedString(i) 
     If (CharUnicodeInfo.GetUnicodeCategory(c) <> UnicodeCategory.NonSpacingMark) Then 
      r.Append(c) 
     End If 
    Next 

    RemoveDiacritics = r.ToString 
End Function 

C#

using System.Text; 
using System.Globalization; 

/// <summary> 
/// Removes the special attributes of the letters passed in the word 
/// </summary> 
/// <param name="word">Word to be normalized</param> 
public String RemoveDiacritics(String word) 
{ 
    String normalizedString = word.Normalize(NormalizationForm.FormD); 
    StringBuilder stringBuilder = new StringBuilder(); 
    int i; 
    Char c; 

    for (i = 0; i < normalizedString.Length; i++) 
    { 
    c = normalizedString[i]; 
    if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) 
    stringBuilder.Append(c); 
    } 

    return stringBuilder.ToString(); 
} 

我希望它能幫助我這樣的人:)

0

有簡單的方法在.NET比較字符串

public static string NormalizeString(string value) 
{ 
    string nameFormatted = value.Normalize(System.Text.NormalizationForm.FormKD); 
    Regex reg = new Regex("[^a-zA-Z0-9 ]"); 
    return reg.Replace(nameFormatted, ""); 
}