Strange behavior of String::Remove method
I am using the String::Remove method to remove the first three characters of a String. However, I am receiving unexpected results from the method.
The first time my function runs it returns the string with the first two characters removed. The next time my function is run it returns the string with only the first character removed.
Are there hidden chars in the String? If so, how do I see them/remove them?
String^ SendCommand(const String^ command)
{
lock l(thisLock);
serialPort->WriteLine((String^) command);
String^ response = serialPort->ReadLine();
if (response[1] == CMD_ERROR_CHAR)
{
// Unknown command was sent
}
response = response->Remove(0, 3);
return response;
}
According to the debugger:
Run1:
(pre-Remove) response = ":A 0 TENTHS"
(post-Remove) response = "0 TENTHS" (no space in front of 0)
Run2:
(pre-Remove) respons开发者_开发问答e = ":A -725"
(post-Remove) response = " -725" (space in front of -)
ANSWER:
I figured it out, finally.
Apparently the Watch window will not hint at escape characters in a String. You will not know they exist unless you specifically index the character.
On Run2 I viewed response[0] in the Watch window and noticed a '\n' character. Even though response showed as ":A -725", it was actually "\n:A -725".
Caused me more trouble than it should have.
精彩评论