Index and length must refer to a location within the string. Parameter name: length

asp.net, c#, substring

Solution

If the length of your string (ddlweek) is 23 characters or less, you will get this error:

    string ddlweek = "12345678901234567890123";//This is NOK
    string a1 = ddlweek.Substring(0, 8);                
    string a3 = ddlweek.Substring(10, 14);
    Console.WriteLine("a1="+a1);
    Console.WriteLine("a3="+a3);
    Console.ReadLine();

The string should be at least 24 characters long.. You might consider adding an `if` to make sure everything is OK..

    string ddlweek = "123456789012345678901234";//This is OK
    string a1 = ddlweek.Substring(0, 8);                
    string a3 = ddlweek.Substring(10, 14);
    Console.WriteLine("a1="+a1);
    Console.WriteLine("a3="+a3);
    Console.ReadLine();

Problem

I'm getting this error: ``` Index and length must refer to a location within the string. Parameter name: length ``` Using this code: ``` string a1 = ddlweek.Text.Substring(0, 8); string a3 = ddlweek.Text.Substring(10, 14); ``` What does this mean?

Original source

Related problems