Replace方法不起作用,C#

时间:2013-11-27 00:50:03

标签: c#

我正在使用C#处理电话号码转换器代码,努力将任何字母字符替换为电话键上的相应数字。在此处的代码中,变量结果已经有10个字符,这是一个混合正如你在代码中看到的那样,我试图在切换案例中使用Replace方法,但它给了我错误的结果。任何想法?谢谢。 对不起,我忘了提到不允许在这个项目中使用数组,只有条件和重复。啊,它是控制台C#。

for (int i = 0; i < result.Length; i++)
        {

            switch (buffer)
            {

                case "A":
                case "B":
                case "C": myNumber = result.Replace(buffer, "2");
                    break;
                case "D":
                case "E":
                case "F": myNumber = result.Replace(buffer, "3");
                    break;

                case "G":
                case "H":
                case "I": myNumber = result.Replace(buffer, "4");
                    break;



                case "J":
                case "K":
                case "L": myNumber = result.Replace(buffer, "5");
                    break;


                case "M":
                case "N":
                case "O": myNumber = result.Replace(buffer, "6");
                    break;

                case "P":
                case "Q":
                case "R":
                case "S": myNumber = result.Replace(buffer, "7");
                    break;

                case "T":
                case "U":
                case "V": myNumber = result.Replace(buffer, "8");
                    break;


                case "W":
                case "X":
                case "Y":
                case "Z": myNumber = result.Replace(buffer, "9");
                    break;

            }


            Console.WriteLine("({0})-{1}-{2}", myNumber.Substring(0, 3), myNumber.Substring(3, 3), myNumber.Substring(6, 4));

4 个答案:

答案 0 :(得分:2)

您将Replace方法结果分配给myNumber,但在下一次循环迭代中,您再次将result作为Replace参数。它无法运作。

怎么样:

private static readonly Dictionary<string, string> PhoneReplacements =
    new Dictionary<string, string>() {
        { "A", "1" }, { "B", "1" }, { "C", "1" },
        { "D", "2" }, { "E", "2" }, { "F", "2" },
        { "G", "3" }, { "H", "3" }, { "I", "3" }
        // (...)
    };

private static string GetPhoneNumber(string number)
{
    foreach(var r in PhoneReplacements)
    {
        number = number.Replace(r.Key, r.Value);
    }
    return number;
}

答案 1 :(得分:1)

String是不可变的。当你这样做

result.Replace(buffer, "9");

结果不会改变。

因此,您只使用替换上一次迭代,而不是代码所做的所有替换的组合。这就是您为代码分配的内容。

而不是

myNumber = result.Replace(buffer, "9");

DO

result = result.Replace(buffer, "9");

甚至,如果你愿意的话

myNumber = result = result.Replace(buffer, "9");

答案 2 :(得分:0)

考虑以下重写......

    string result = "1800CALLME";
    string myNumber = string.Empty;
    for (int i = 0; i < result.Length; i++) 
    {
    string buffer = result[i].ToString(); 
    switch (buffer)
    {

        case "A":
        case "B":
        case "C": myNumber += "2";
            break;
        case "D":
        case "E":
        case "F": myNumber += "3";
            break;

        case "G":
        case "H":
        case "I": myNumber += "4";
            break;

        case "J":
        case "K":
        case "L": myNumber += "5";
            break;

        case "M":
        case "N":
        case "O": myNumber += "6";
            break;

        case "P":
        case "Q":
        case "R":
        case "S": myNumber += "7";
            break;

        case "T":
        case "U":
        case "V": myNumber += "8";
            break;


        case "W":
        case "X":
        case "Y":
        case "Z": myNumber += "9";
            break;

    }


    Console.WriteLine("({0})-{1}-{2}", myNumber.Substring(0, 3), myNumber.Substring(3, 3), myNumber.Substring(6, 4));

祝你好运!

答案 3 :(得分:0)

请记住在构建字符串时使用StringBuilder,尤其是在迭代中构建字符串时。

private static readonly Dictionary<Char, Byte> mappings = new Dictionary<Char, Byte>() {
    { 'A', 2 }, { 'B', 2 }, { 'C', 2 },
    { 'D', 3 }, { 'E', 3 }, { 'F', 3 },
    { 'G', 4 }, { 'H', 4 }, { 'I', 4 },
    { 'J', 5 }, { 'K', 5 }, { 'L', 5 },
    { 'M', 6 }, { 'N', 6 }, { 'O', 6 },
    { 'P', 7 }, { 'Q', 7 }, { 'R', 7 }, { 'S', 7 },
    { 'T', 8 }, { 'U', 8 }, { 'V', 8 },
    { 'W', 9 }, { 'X', 9 }, { 'Y', 9 }, { 'Z', 9},
    { ' ', 0 }
};

private static string StringToKeystrokes(string s)
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in s)
    {
        if (mappings.ContainsKey(c))
        {
            sb.Append(mappings[c]);
        }
    }
    return sb.ToString();
}

使用代码。

string s = "ABC DEF GHI JKL MNO PQRS TUV WXYZ";
Console.WriteLine(s);
Console.WriteLine(StringToKeystrokes(s));