Why does this happen?
I am getting a string from another class to compare it to a current string, but the if statement didn’t work because there is something wrong with the string. When I tried to check the length, they differ. How is this possible?
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 "";}
}
Either something strange is going on, or I’m losing it.
This has been done in a CS script. (Used by Unity.)
UPDATE:
Running the code provided by Jon Skeet, this is my Results
Lenght: 3
receivedCom[0] = 103
receivedCom[1] = 111
receivedCom[2] = 13
UPDATE : How i Came to get a “carriage return”
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();
}
You’ve shown there isn’t any whitespace. That doesn’t mean there isn’t a non-printable character.
The simplest diagnostic is to just print out the “odd” Unicode value:
I’m guessing it’ll be 0, and it’s just an off-by-one error in how you’re reading the data.
EDIT: Of course to show exactly what’s in the string, just print everything:
If you could edit the result of that into the question, we can make progress.