I have a string where the first 5 characters are never empty and from char 6 to the end data is variable leght. Something like this:
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
{
//...
}
Output:
inData: {1112Test}
origMsg: 2
new inData: Test
This code throws a ArgumentOutOfRangeException: Index and length must refer to a location within the string. How can I solve this?
inDatais. Your code simply won't throw anArgumentOutOfRangeExceptionwith the value you provided.