C# - Substring: index and length must refer to a location within the string

index and count must refer to a location within the string
substring in c#
c# left string
how to get specific character from string in c#
c# substring length greater than string
c# substring method
substring c# after character
c# truncate string

I have a string that looks like

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

"www.example.com/" is 18 fixed in length. I want to get the "aaa/bbb" part from this string (The actual url is not example nor aaa/bbb though, the length may vary)

so here's what I did:

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

Then I got the exception: index and length must refer to a location within the string. What's wrong with my code and how to fix it? Thanks in advance.


The second parameter in Substring is the length of the substring, not the end index.

You should probably include handling to check that it does indeed start with what you expect, end with what you expect, and is at least as long as you expect. And then if it doesn't match, you can either do something else or throw a meaningful error.

Here's some example code that validates that url contains your strings, that also is refactored a bit to make it easier to change 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

and length must refer to a location within the string , It means that your ddlweek. Text string contains less number of characters than what you asked for in Substring(index, length) . It just means you're asking for a substring of ddlweek that doesn't exist (ie, it's more than 24 characters in length). System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) at System.String.Substring(Int32 startIndex, Int32 length) at QuoteExtractor.frmQuoteExtractor.cmdProcess_Click(Object sender


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

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

If the length of the substring can vary you need to calculate the length.

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

In the end it will look something like this

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

[Solved] Index and length must refer to a location within the string , The second parameter of String.Substring is length. I changed the 6 to a 3. Hide Copy Code. string myString = TextBox1.Text.ToString(); string  string newString = url.Substring(18, url.Length - 4); Then I got the exception: index and length must refer to a location within the string. What's wrong with my code and how to fix it? Thanks in advance.


How about 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

string.Substring(); Index and length must refer to location, Hi, i got the error in below code. what i did wrong Error : Index and length must refer to a location within the string.\r\nParameter name: length  "Index and length must refer to a location within the string." when i'm giving in line2 the same substring values like in line1 it works. How can i split this line into two lines?


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

and length must refer to a location within the string. Parameter name, int lenPrice = T10.Text.Length; string strPrice = Convert.ToString(T10.Text); strPrice= strPrice.Substring(1,lenPrice); oUserPrice.Price= Convert. Re: Index and length must refer to a location within the string.Parameter name: length problem :-index and length must refer to a location within the string. parameter name length in c# Index and length must refer to a location within the string.


Try This:

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

FIX: "Index and length must refer to a location within the string" error , FIX: "Index and length must refer to a location within the string" error InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean  Console.WriteLine(str3) Catch e As ArgumentOutOfRangeException Console.WriteLIne(e.Message) End Try End Sub End Class ' The example displays the following output: ' True ' True ' Index and length must refer to a location within the string.


String.Substring Method (System), The substring starts at a specified character position and continues to the end of the True // True // Index and length must refer to a location within the string. Index and count must refer to a location within the string. problem :-index and length must refer to a location within the string. parameter name length in c# Index and length must refer to a location within the string.


MVC - C#, ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(  Hi, i want to read a text file, line by line. My program reads first line, then (when they like to read second line) they give mi the exception: "index and length must refer to a location within the string". Code:


[Error] System.ArgumentOutOfRangeException: Index and length , Error: Index and length must refer to a location within the string. Parameter String.Substring(Int32 startIndex, Int32 length) +226 Aspose. string val = elespt[0].ToString(); p1 = val.Substring(l1, l2); elespt[0] is " this page, it means the vendor you purchased this e-book from did not deliver the correct file.