c#字符串混乱

时间:2012-04-26 03:25:31

标签: c# string

我对C#很新,对某些事感到困惑.....

让我告诉你发生了什么,希望你们能告诉我这里做错了什么。

string incomming = Encoding.UTF8.GetString(bytes);
//MessageBox.Show(incomming); shows me the string "stop", No problem

executeCommand(incomming);

public void executeCommand(string action)
{
    MessageBox.Show(action + " was recieved"); // shows the string "stop", No  problem here... that works

    switch (action)
    { 
        case "start":
            MessageBox.Show("start was recieved"); //shows nothing
            break;

        case "stop":
             MessageBox.Show("stop was recieved"); //shows nothing
             break;
    }            
}

2 个答案:

答案 0 :(得分:1)

如果不知道正在转换为String的Byte数组的内容,很难给你任何帮助。但是这里有几件事要尝试。

  1. 您可以在executeCommand(incomming)上设置断点并在监视窗口中键入incomming.ToCharArray(),您需要点击值列中的绿色圆圈,然后才能看到除了字符串。这应该让你知道你在处理什么。

  2. 您可以使用String.Contains方法搜索incomming以查找您要查找的字符串的匹配项。


  3. if (action.Contains("stop"))
        MessageBox.Show("stop was recieved");
    else if (action.Contains("start"))
        MessageBox.Show("start was recieved");
    

答案 1 :(得分:0)

对此唯一合理的解释是,你的一个“停止”字符串有一个与英语不同的字母,但外观相同。在这种情况下:

"stop" != "stоp"  

是真的;

相关问题