Question

I 've an actual function which replace specific chars by others specific chars in a string. It works fine but I find this function is ugly. How I can replace this function by a more elegant function ?

private string CorrectString(string data)
{
    string retData = string.Empty;

    for (int j = 0; j < data.Length; j++)
    {
        char myChar = data[j];

        switch (myChar)
        {
            case 'à': 
                myChar = '@';
                break;
            case 'ç': 
                myChar = '\\';
                break;
            case '[': 
                myChar = '°';
                break;
            case '!': 
                myChar = '!';
                break;
            case 'é': 
                myChar = '{';
                break;
            case 'è': 
                myChar = '}';
                break;
            case ']': 
                myChar = '§';
                break;
            case '¦': 
                myChar = 'ù';
                break;
            case '`': 
                myChar = 'µ';
                break;
            case '#': 
                myChar = '£';
                break;
            case '@': 
                myChar = 'à';
                break;
            case '°': 
                myChar = '[';
                break;
            case '¤': 
                myChar = '€';
                break;
            case 'µ': 
                myChar = '`';
                break;
            case '~':
                myChar = '"';
                break;
            case 'Ý': 
                myChar = 'Ý';
                break;
            case '¢': 
                myChar = '¢';
                break;
            case '£': 
                myChar = '#';
                break;
            case '§': 
                myChar = ']';
                break;
            case '¬': 
                myChar = '¬';
                break;
            case '|': 
                myChar = '|';
                break;
            case '"':
                myChar = '~';
                break;
            case '{': 
                myChar = 'é';
                break;
            case '}': 
                myChar = 'è';
                break;
            case 'ù': 
                myChar = '¦';
                break;
            case '\\':
                myChar = 'ç';
                break;
        }

        retData = retData.Insert(j, myChar.ToString());
    }

    return retData;
}
Était-ce utile?

La solution

Solution 1

You can simply hold your characters in two strings:

var toTranslate = "àç[!é...";
var translateTo = @"@\°...";

and then translate as you find the characters:

int index = toTranslate.IndexOf(myChar);
if (index > -1)
{
    myChar = translateTo[index];
}

Solution 2

Another, efficient but less readable way would be to store a Dictionary<char, char> and to use it like so:

myDictionary['à'] = '@';
myDictionary['ç'] = '\\';
...
if (myDictionary.Keys.Contains(myChar))
{
    myChar = myDictionary[myChar];
}

Autres conseils

If speed is not a concern, you might want to create a Dictionary<char, char> and for each key of the dictionary, check whether it is equal to a current character. If it is, then replace it by the appropriate value. Advance one character and continue.

Or a sequence of Replace statements:

myCleanString= Regex.Replace(data, "à", @"@")
                        .Replace("ç", "\\")
                        .Replace("[", "°") ...
_MyString1 = "@\\°!"; // those you put in the partial class or public class of your program
_MyString1 = "@\\°!";          
string c = _MyString1.Substring(_MyString.IndexOf("à"),1); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top