• Register
0 votes
91 views

Problem :

I am facing 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?

6 5 3
7,540 points

2 Answers

0 votes

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 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).

9 7 4
38,600 points
0 votes

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);

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 89 views
89 views
Problem : I am new to ASP.Net and I have an ASP.Net application which runs correctly on my local development machine. But when I try to run this application online, it shows me following error Format of the initialization string does not conform to specification starting at index 0 I don’t have any clue about this error. Does anybody have any idea regarding solution on this error?
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 7 views
7 views
How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.?
asked Aug 28 Aliza313 720 points
0 votes
1 answer 59 views
59 views
Problem : I want to deploy my ASP.NET application. I have tried deploying my site to IIS, but when I visit it with the browser, it gives me following error : Server Error 500 - Internal server error. There is a problem with the resource you are looking for ... error: The page cannot be displayed because an internal server error has occurred. I want to see the actual issue behind my server error?
asked Dec 7, 2019 alecxe 7.5k points
0 votes
1 answer 5 views
5 views
Problem: Hi, I want to take multiple employee ids in one shot from the user and need to assign them to list .in asp.net, pls help me to sort it out. What I have tried. I have created a text box and tried with validator but it didn't worked out.
asked Sep 14 boris elin 620 points