是否可以将一个字母(例如“ A”)转换为符号,但仍然能够将其转换回原始格式(区分大小写)?

例如:

public void doIt(string input)
{
   // do magic work


   // display result
}

*Input = A
*Result = ^$

最佳答案

string ORIGINAL_CHARS = "ABCD";
string MAGICAL_CHARS = "$#@%";

public void DoMagic(string input)
{
    string output = input;
    for (int i = 0; i < ORIGINAL_CHARS.Length; i++)
    {
        if(i > MAGICAL_CHARS.Length)
        {
            continue;
        }
        output = output.Replace(ORIGINAL_CHARS[i], MAGICAL_CHARS[i]);
    }
    return output;
}

public void UndoMagic(string input)
{
    string output = input;
    for (int i = 0; i < MAGICAL_CHARS.Length; i++)
    {
        if(i > ORIGINAL_CHARS.Length)
        {
            continue;
        }
        output = output.Replace(MAGICAL_CHARS[i], ORIGINAL_CHARS[i]);
    }
    return output;
}

关于c# - 魔术字母,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5336021/

10-14 09:18