chevron_left

Problem:

I am facing the below error:

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

Parameter name: length

I am using below code:

string s1 = ddlweek.Text.Substring(0, 8);                
string s3 = ddlweek.Text.Substring(10, 14);

How can I resolve the error?

Solution :

If in any case the length of your string (ddlweek) is 23 characters or less than 23 characters, then you will get error:

Please see the below explanation for you.

string ddlweek = "12345678901234567890123";//This is clearly Not Ok
string s1 = ddlweek.Substring(0, 8);                
    string s3 = ddlweek.Substring(10, 14);
    Console.WriteLine("s1="+s1);
    Console.WriteLine("s3="+s3);
    Console.ReadLine();

So your string must be at least 24 characters long.. So you must consider adding the if to make sure that everything is OK. As explained for you below :

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

OR

It clearly means that your ddlweek.Text string contains the less number of characters than what you asked for in the Substring(index, length).

Example as below:

if (ddlweek.Text.Length >= 8)

    string s1 = ddlweek.Text.Substring(0, 8); 

OR

It just clearly means you are asking for a substring of ddlweek that does not exist (it is more than 24 characters in length).

Another Solution:
The second parameter in Substring is the length of the substring, not the closing index.

You must possibly add handling to check that it does truly begin with what you expect, end with what you expect, and is at least as long as you expect. And then in case it doesn't match, you can either do something else or throw a significant error.

Here's some example code that affirms that url holds your strings, that further is refactored a bit to make it simplier to alter the prefix/suffix to strip:

var prefix = "www.example.com/";
var suffix = ".jpg";
string url = "www.example.com/aaa/bbb.jpg";

if (url.StartsWith(prefix) && url.EndsWith(suffix) && url.Length >= (prefix.Length + suffix.Length))
{
    string newString = url.Substring(prefix.Length, url.Length - prefix.Length - suffix.Length);
    Console.WriteLine(newString);
}
else
    //handle invalid state

Your fault is the parameters to Substring. The first parameter must be the start index and the second must be the length or offset from the startindex.

string newString = url.Substring(18, 7);

In case the length of the substring can vary you require to calculate the length.

Something in the way of (url.Length - 18) - 4 (or url.Length - 22)

In the end, it will seem something like this

string newString = url.Substring(18, url.Length - 22);

Or, something like this :

string url = "http://www.example.com/aaa/bbb.jpg";
Uri uri = new Uri(url);
string path_Query = uri.PathAndQuery;
string extension =  Path.GetExtension(path_Query);

path_Query = path_Query.Replace(extension, string.Empty);// This will remove extension

You require to find the position of the first /, and then calculate the portion you want:

string url = "www.example.com/aaa/bbb.jpg";
int Idx = url.IndexOf("/");
string yourValue = url.Substring(Idx + 1, url.Length - Idx - 4);

Attempt This:

int positionOfJPG=url.IndexOf(".jpg");
 string newString = url.Substring(18, url.Length - positionOfJPG);
string newString = url.Substring(18, (url.LastIndexOf(".") - 18))

In case the length of your string (ddlweek) is 23 characters or less, you will obtain 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 must be at least 24 characters long.. You might take into account including an if to ensure that 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();

It imply that your ddlweek.Text string holds less number of characters than what you requested for in Substring(index, length).

Example:

if (ddlweek.Text.Length >= 8)
    string a1 = ddlweek.Text.Substring(0, 8);  

This error happens at the time you exceed your end limit of total character. for example

 string name = "iLovePakistan"; // Here i want to print only Pakistan

        string name2 = name.Substring(5, 150); // this code will throw same error. just replace 150 with name.Length - 5

        string name3 = name.Substring(5, name.Length - 5); // i skip firt 5 charchers then name.Length-5 means print rest 8 characters.

        string name4 = name.Substring(5, 8); // This will do exactly as name3
        Console.WriteLine(name4);

.

string myString = TextBox1.Text.ToString();
string substring = myString.Substring(0, 3);
string substring2 = myString.Substring(3, 3);
 

I hope this will help you, Thanks

More Posts

Index and length must refer to a location within the string. parameter name: length. Farkhanda Athar - Aug 29, 2020
Cannot be accessed with an instance reference, qualify it with a type name instead Alecxe01 - Nov 12
excel found unreadable content in do you want to recover the contents of this workbook SharadMagar450 - Sep 16
The entity type applicationuser is not part of the model for the current context. Alecxe01 - 5 days
Dynamically setting ColumnSpan on a GridView intermittently breaks layout Tushar Shuvro - Jul 17, 2020
.NET :- Matching object to Ojects in Hashset using linq Tushar Shuvro - Jul 17, 2020
How to remove objects in recycle bin Sitecore CMS with powershell scrript? Tushar Shuvro - Jul 17, 2020
gridview is displaying incorrect data from database Tushar Shuvro - Jul 17, 2020
the remote connection was not made because the name of the remote access server did not resolve SharadMagar450 - Aug 31
the following feature couldn't be installed .net framework 3.5 (includes .net 2.0 and 3.0) SharadMagar450 - Sep 4