On running below program error is shown
**
Unhandled Exception : systemArgumentOutofRange:Index and length must
refere to location within string.
**
string input = "OneTwoThree sdfhguiosdrb asdhfuiaserthf aseiryaseuigraweui";
int size = input.Length;
if (input.Length > 30)
{
msg1 = input.Substring(0, 30);
msg2 = input.Substring(msg1.Length, size);
Console.WriteLine("Message: {0}", msg1);
Console.WriteLine("\nMessage: {0}", msg2);
}
else
Console.WriteLine(input);
Please help what I am doing wrong.
you are doing wrong on this line
what you are doing here is, creating a substring
msg2that is longer then the size of your first stringmsg1,here, your
msg1.Lengthequals30and yoursizeequals58means that you are trying to create a string havingstartingIndex = 30andLength = 58and this will be impossible in this condition, because the Length of your second stringmsg1is30and it is less thenSize i.e. 58.Albin Sunnanbo answer is good as per your requirement.i.e.