我有一个字符串,其中前 5 个字符从不为空,并且从 char 6 到结尾数据是可变长度。像这样的东西:
string inData = comPort1.ReadExisting();
//Console.WriteLine("inData: " + inData);
string origMsg = inData.Substring(4, 1);
//Console.WriteLine("origMsg: " + origMsg);
string seAnex = inData.Substring(5, 15); // ArgumentOutOfRangeException
inData = inData.Substring(5, inData.Length - 8);
//Console.WriteLine("new inData: " + inData);
if (seAnex == "some_text_15_ch")
{
//...
}
else
{
//...
}
输出:
inData: {1112Test}
origMsg: 2
new inData: Test
此代码抛出一个ArgumentOutOfRangeException: Index and length must refer to a location within the string
. 我该如何解决这个问题?