Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring index and length must refer to a location within the 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?

like image 291
Manto Avatar asked Jun 21 '12 23:06

Manto


People also ask

What does this mean index and length must refer to a location within the string parameter name length?

It means that your ddlweek. Text string contains less number of characters than what you asked for in Substring(index, length) . Show activity on this post. It just means you're asking for a substring of ddlweek that doesn't exist (ie, it's more than 24 characters in length).

How do I limit the length of a string in C#?

Strings in C# are immutable and in some sense it means that they are fixed-size. However you cannot constrain a string variable to only accept n-character strings. If you define a string variable, it can be assigned any string.


3 Answers

The second parameter in Substring is the length of the substring, not the end index (in other words, it's not the length of the full string).

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
like image 162
Tim S. Avatar answered Oct 09 '22 20:10

Tim S.


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);
like image 26
MAV Avatar answered Oct 09 '22 20:10

MAV


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
like image 6
HatSoft Avatar answered Oct 09 '22 21:10

HatSoft