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.
You can use padding right.
Happy Coding!
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.
If the length of the substring can vary you need to calculate the length.
Something in the direction of
(url.Length - 18) - 4
(orurl.Length - 22
)In the end it will look something like this
You need to find the position of the first
/
, and then calculate the portion you want:How about something like this :
Try This: