简体   繁体   English

如何将string.replace()与char一起使用?

[英]How do I use string.replace() with chars?

So full disclousre this is homework. 因此,非常不屑一顾,这是家庭作业。 Anyway trying to make a morse code converter and I am just stuck on this last issue. 无论如何试图做一个莫尔斯电码转换器,我只是停留在最后一个问题上。 I want to use chars and then use string.replace but I can't since my dictionary is all strings. 我想使用字符,然后使用string.replace,但是我不能,因为我的字典都是字符串。 I want to use chars though. 我想用字符。 So how would I get around this? 那么我该如何解决呢?

public void InputReader()
{
    string inputForTranslating = inputForTranslator.Text;

    Dictionary<string, string> morseDictionary = new Dictionary<string, string>
    {
        { " ", " " }, { ",", "--..--" }, { ".", ".-.-.-" }, { "\\?", "..--.." }, { "0", "-----" }, { "1", ".----" }, { "2", "..---" }, { "3", "...--" },
        { "4", "....-" }, { "5", "....." }, { "6", "-...." }, { "7", "--..." }, { "8", "---.." }, { "9", "----." }, { "A", ".-" },
        { "B", "-..." }, { "C", "-.-." }, { "D", "-.." }, { "E", "." }, { "F", "..-." }, { "G", "--." }, { "H", "...." }, { "I", ".." },
        { "J", ".---" }, { "K", "-.-" }, { "L", ".-.." }, { "M", "---" }, { "N", "-." }, { "O", "---" }, { "P", ".--." }, { "Q", "--.-" },
        { "R", ".-." }, { "S", "..." }, { "T", "-" }, { "U", "..-" }, { "V", "...-" }, { "W", ".--" }, { "X", "-..-" }, { "Y", "-.--" },
        { "Z", "--.." }
    };

    char[] charArray = inputForTranslating.ToCharArray();
    for (int i = 0; i < charArray.Length; i++)
    {

        outPutTranslation.Text = outPutTranslation.ToString().Replace(morseDictionary.Keys, morseDictionary.Values); ////This is where the error occurs "cannot convert from 'System.Collections.Generic.Dictionary<string, string>.KeyCollection' to 'char'"
    }
}

Replace takes strings/chars as parameters, not a collection of keys or values. Replace将字符串/字符作为参数,而不是键或值的集合。 In this case, you don't even need the Replace , you can just add the values based on the keys. 在这种情况下,您甚至不需要Replace ,只需添加基于键的值即可。
Also, your outPutTranslation.Text will only have the last char. 另外,您的outPutTranslation.Text将仅具有最后一个字符。

Dictionary<string, string> morseDictionary = new Dictionary<string, string>
{
    { " ", " " }, { ",", "--..--" }, { ".", ".-.-.-" }, { "\\?", "..--.." }, { "0", "-----" }, { "1", ".----" }, { "2", "..---" }, { "3", "...--" },
    { "4", "....-" }, { "5", "....." }, { "6", "-...." }, { "7", "--..." }, { "8", "---.." }, { "9", "----." }, { "A", ".-" },
    { "B", "-..." }, { "C", "-.-." }, { "D", "-.." }, { "E", "." }, { "F", "..-." }, { "G", "--." }, { "H", "...." }, { "I", ".." },
    { "J", ".---" }, { "K", "-.-" }, { "L", ".-.." }, { "M", "---" }, { "N", "-." }, { "O", "---" }, { "P", ".--." }, { "Q", "--.-" },
    { "R", ".-." }, { "S", "..." }, { "T", "-" }, { "U", "..-" }, { "V", "...-" }, { "W", ".--" }, { "X", "-..-" }, { "Y", "-.--" },
    { "Z", "--.." }
};

string output = "";

foreach (char c in inputForTranslating.ToCharArray())
{
    output += morseDictionary[c];
}

outPutTranslation.Text = output;

Well, string.Replace() works both with two chars or two strings. 好吧, string.Replace()可以使用两个字符或两个字符串。 The error clearly states that morseDictionary.Keys is not a string. 该错误明确指出morseDictionary.Keys不是字符串。 Neither is morseDictionary.Values . morseDictionary.Values都不是。 And it's right, they are the list of keys and values of the dictionary! 是的,它们是字典的键和值的列表!

There's another mistake in that code. 该代码还有另一个错误。 You're converting your input to a char array, and then iterating each character, and trying to replace there. 您正在将输入转换为char数组,然后迭代每个字符并尝试在其中替换。 Think about what it's doing: 考虑一下它在做什么:

If you have -.- , in the first iteration you will search over - , in the second over . 如果有-.- ,则在第一次迭代中将搜索- ,在第二次搜索中. and lastly over - . 最后- You'll never be able to find the K . 您将永远找不到K

You should iterate your dictionary, and search each word in the whole string. 您应该迭代字典,并在整个字符串中搜索每个单词。

foreach(string key in morseDictionary) {     
    //for morse->letter   
    inputForTranslating=inputForTranslating.Replace(morseDictionary[key],key);
    //for letter->morse
    inputForTranslating=inputForTranslating.Replace(key,morseDictionary[key]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM