0

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?

32
  • 8
    Have you read the documentation? msdn.microsoft.com/en-us/library/aka44szs.aspx Commented Sep 20, 2013 at 15:42
  • 1
    Are you sure that your input is always at least 5 characters long? Run it in debugging or output when it fails. (also, are you trimming whitespace? Perhaps that is what's throwing it off. Your initial values are length 5 or more, then you trim reducing its length.) Commented Sep 20, 2013 at 15:46
  • 1
    @mafap OK, well for the 4th time, you need to find out what inData is. Your code simply won't throw an ArgumentOutOfRangeException with the value you provided. Commented Sep 20, 2013 at 16:25
  • 1
    I've had this trouble in the past. The problem is that substring is unkind, it doesn't use intelligence. it does exactly what you tell it to and if the input string is too large or small it throws an exception. Commented Sep 20, 2013 at 16:54
  • 1
    oops i forgot about the } bracket at the end of your strings. Try indata.Substring(5, indata.Length - 6) instead. That'll get all the characters from the 5th index to the last but one character. Commented Sep 20, 2013 at 17:13

3 Answers 3

2
string origMsg = inData.Substring(4, 1);

"startIndex cannot be larger than length of string"

in other words,

4 cannot be larger than length of inData

4 is larger than length of inData

inData.Length is less than 4

Sign up to request clarification or add additional context in comments.

1 Comment

pretty sound logic to me
1

I'm not sure what do you want to do with your code. But, if just to resolve the exception. You can fix like this:

        string inData = comPort1.ReadExisting();
        //Console.WriteLine("inData: " + inData);
        if (inData.Length >= 5)
        {
            string origMsg = inData.Substring(4, 1);
            //Console.WriteLine("origMsg: " + origMsg);
            //string seAnex = inData.Substring(5, 15);           // ArgumentOutOfRangeException
            string seAnex = inData.Substring(5, inData.Length - 5);
            //inData = inData.Substring(5, inData.Length - 8);
            //Console.WriteLine("new inData: " + inData);

            if (seAnex == "some_text_15_ch")
            {
                //...
            }
            else
            {
                //...
            }
        }

I guess your purpose can be get data information from a message have format like "{111abcxzy}" in a long data string "{111abcxzy}{111abcxzy}{..." that received from COM communication ?

1 Comment

Yes, something like that but it's solved now. Like I said in my last comment this worked with inData.Substring(5, inData.Length - 8);, inData is at least equal to "{1112}\r\n". For the if-else statment I used inData.StartsWith("some_text_15_ch").
0

I would bet that you do not have the string value you say that you do. If it were in fact 5+ characters long, you would not have and ArgumentOutOfRange exception when you call SubString(4,1) on it. Print out the value or inspect it in the debugger to confirm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.