43

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Manto
  • 1,069
  • 2
  • 15
  • 34

9 Answers9

70

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
TylerH
  • 20,799
  • 66
  • 75
  • 101
Tim S.
  • 55,448
  • 7
  • 96
  • 122
8

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);
MAV
  • 7,260
  • 4
  • 30
  • 47
  • The length of "aaa/bbb" part varies from time to time so it's not exactly 7. But thanks for answering my question. upvoted your answer :) – Manto Jun 21 '12 at 23:37
  • 22 is correct, because you want to know the length, not the index – walther Jun 21 '12 at 23:45
7

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
Kenster
  • 23,465
  • 21
  • 80
  • 106
HatSoft
  • 11,077
  • 3
  • 28
  • 43
4

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);
Kenster
  • 23,465
  • 21
  • 80
  • 106
Ken White
  • 123,280
  • 14
  • 225
  • 444
2

Try This:

 int positionOfJPG=url.IndexOf(".jpg");
 string newString = url.Substring(18, url.Length - positionOfJPG);
Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39
2
string newString = url.Substring(18, (url.LastIndexOf(".") - 18))
Bastardo
  • 4,144
  • 9
  • 41
  • 60
0

Here is another suggestion. If you can prepend http:// to your url string you can do this

  string path = "http://www.example.com/aaa/bbb.jpg";
  Uri uri = new Uri(path);            
  string expectedString = 
      uri.PathAndQuery.Remove(uri.PathAndQuery.LastIndexOf("."));
Kenster
  • 23,465
  • 21
  • 80
  • 106
Prashanth Thurairatnam
  • 4,353
  • 2
  • 14
  • 17
0

You need to check your statement like this :

string url = "www.example.com/aaa/bbb.jpg";
string lenght = url.Lenght-4;
if(url.Lenght > 15)//eg 15
{
 string newString = url.Substring(18, lenght);
}
0

Can you try this ?

string example = url.Substring(0,(url.Length > 18 ? url.Length - 4 : url.Length))

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 12 '22 at 06:01