为什么会这样?
我从另一个类中获取一个字符串以将其与当前字符串进行比较,但是 if 语句不起作用,因为该字符串有问题。当我尝试检查长度时,它们有所不同。这怎么可能?
receivedCom = "去";
public string checkaction(string receivedCom)
{
print ("-------" + receivedCom + "-------" + receivedCom.Length); //Just to show there isnt any white spaces behind or infront --> OUTPUT IS "-------go-------3"
print (receivedCom + receivedCom.Replace(" ", "").Length); //Tried removing white spaces if there were any --> OUTPUT "go3"
string x = receivedCom.Remove(receivedCom.Length-1);
print (x + " " +x.Length); --> OUTPUT IS "go 2" (Correct lenght, but if still doesnt want to work with it)
if("go".Equals(x)){
return "yes";
}
else{return "";}
}
要么发生了一些奇怪的事情,要么我正在失去它。
这是在 CS 脚本中完成的。(由 Unity 使用。)
更新:
运行 Jon Skeet 提供的代码,这是我的结果
Lenght: 3
receivedCom[0] = 103
receivedCom[1] = 111
receivedCom[2] = 13
更新:我是如何获得“回车”的
void Start () {
player = GameObject.FindWithTag("Player");
script = (PlayerScript) player.GetComponent(typeof(PlayerScript));
Process p = new Process();
p.StartInfo.FileName = @"F:\ReceiveRandomInput.exe"; //This exe generates random strings like "go" "start" etc as a console application
p.StartInfo.Arguments = "arg0 arg1 arg2 arg3 arg4 arg5";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//add event handler when output is received
p.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
data = e.Data; //THIS DATA is what i sent though to the other class (one with the carriage return in
received = true;
};
p.Start();
p.BeginOutputReadLine();
}