• Register
0 votes
282 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

Please log in or register to answer this question.

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 129 views
129 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 49 views
49 views
Problem: Any help: sqldatetime overflow. must be between 1/1/1753 12:00:00 am and 12/31/9999 11:59:59 pm.
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
1 answer 5 views
5 views
Problem: I am getting the error : Maximum request length exceeded when I am trying to upload a video in my site. How do I fix this?
asked Mar 21 ummesalma 25.2k points
0 votes
1 answer 2 views
2 views
Problem WCF - More than one endpoint configuration for that contract was found - Error?How to fix this error Why I got the error :
asked Apr 7 neeraj 10.5k points
0 votes
1 answer 13 views
13 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, 2020 Aliza313 720 points
0 votes
1 answer 4 views
4 views
Problem: Everything works fine, except when I go to access Property1. The intellisense only gives me "Equals, GetHashCode, GetType, and ToString" as options. When I mouse over the oItem.Property1, Visual Studio gives me this explanation: MemberMyDataLayer.Section1.MyClass.MyItem.Property1. ... a type name instead I am unsure of what this means, I did some googling but wasn't able to figure it out.
asked Mar 30 ummesalma 25.2k points
0 votes
1 answer 523 views
523 views
Problem : I am getting into the C# coding but I am having following issue: I have below code on the UserControl: using MyFirstDataLayer.Section1; public class MyFirstClass { protected void MyFirstMethod { MyFirstClass.MyFirstItem oItem = new MyFirstClass.MyFirstItem(); oItem = ... name instead I am not sure of what this actually means, I did some research but was unable to figure it out.
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 71 views
71 views
Problem : I am facing below error if I try to export datagrid to the excel. &ldquo;Control 'gridview1' of type 'gridview' must be placed inside a form tag with runat=server.&rdquo; Please find below my code : <form runat="server" id="form1"> <div runat="server" id=" ... TextBox> </form> My GridView is within the from tag with the runat="server", but still I am facing above error. How to fix this?
asked Feb 17, 2020 mphil 2.3k points
3 votes
1 answer 269 views
269 views
Problem : I am very new to Crystal Report. I have recently got one project which needs me to make a crystal report in the asp.net for my web application. My previous reports were working very well. But recently my reports have started showing me below error message. Invalid index ... .50727.4927, VS:- VS 2008, Database:- MS SQL Express 2005, OS:- Windows 7 Can somebody help me in fixing my issues?
asked Mar 24, 2020 morrisBson 3.2k points