爲什麼會發生這種情況?字符串的長度比字符串的長度長
我從另一個類中獲取一個字符串以將其與當前字符串進行比較,但if語句無法正常工作,因爲字符串出現問題。當我試圖檢查長度時,它們有所不同。這怎麼可能?
receivedCom =「go」;
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使用。)
UPDATE:
運行由喬恩斯基特提供的代碼,這是我的結果
Lenght: 3
receivedCom[0] = 103
receivedCom[1] = 111
receivedCom[2] = 13
UPDATE:我變成了得到一個 「回車」
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();
}
您是否在刪除最後一個字符之前在字符串*上運行了該測試? – Esailija
這是一個回車符。你可以刪除它,或修剪()字符串 – TheEvilPenguin
我更新了測試結果(有一個變量「去」編碼爲以前的測試) – Ruan