2009-02-09 6 views
3

dans .NET est-il une fonction qui renvoie la lettre racine (la lettre sans attributs spéciaux comme cédille), un peu:Y at-il une fonction qui retourne la lettre racine pour les caractères spéciaux?

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 

Ce manque de code des lettres, mais il est seulement pour exemple :)

Répondre

2

Soit dit en passant (sans aucun rapport avec la question), votre code fonctionne sur les chaînes. Ce n'est pas seulement moins efficace, cela n'a pas vraiment de sens puisque vous êtes intéressé par les caractères individuels plutôt que par des chaînes, et ce sont des types de données distincts dans .NET.

Pour obtenir un seul caractère littéral plutôt que d'une chaîne littérale, ajoutez à votre c littérale:

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

pris de la réponse Chetan Sastry, ici je vous donne le code VB.NET et C# une copie de sa réponse :) GRANDE

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(); 
} 

J'espère que cela aide quelqu'un comme moi :)

0

Il est comme méthode simple de comparer chaîne dans .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, ""); 
} 
Questions connexes