0

I have a string like this:

"http://localhost:55164/Images/photos/2/2.jpg"

I need to retrieve the filename and the 2 out of the /2/ and put them into their own strings. I've been messing around with StringBuilder and replace and substr to no avail since the filename length is variable. Anyone have a quick way to do this?

Thanks

1
  • Regex with groups, or you could split by "/" Commented Aug 18, 2011 at 11:02

6 Answers 6

6
string link = "http://localhost:55164/Images/photos/2/2.jpg"; // your link
string[] x = link.Split('/'); // split it into pieces at the slash
string filename = (x.Length >= 1) ? x[x.Length - 1] : null; // get the last part
string dir = (x.Length >= 2) ? x[x.Length - 2] : null; // get the 2nd last part

Edit, checked the array length before trying to access it's pieces like someone suggested below in the comments.

Sign up to request clarification or add additional context in comments.

2 Comments

Note: in real life, you'd actually check the length of x rather than blindly assuming you have at least 2 elements.
@David Kemp thanks, overlooked that fact. fixed it now though.
1

You could cheat and use the Path class. This is easier and adds extra readability at the same time.

string path = "http://localhost:55164/Images/photos/2/2.jpg";
Console.WriteLine(Path.GetFileName(path));
string[] dirSplit = Path.GetDirectoryName(path).Split('\\');
Console.WriteLine(dirSplit[dirSplit.Length - 1]);

1 Comment

umm nice, did not know about the path class before. I like the first part, but seems a bit redundant to use it (and) split the string, when splitting can achieve both things.
1

I would suggest using the Path class:

string filename = Path.GetFileName(s);
string dir = Path.GetDirectoryName(s).GetFileName(s);

Comments

0

One quick way you could do with would be to split the string by the forward slash.

In this way you'll know that the last item in the array and the second-to-last item will be what you require.

Such that:

string url = "http://localhost:55164/Images/photos/2/2.jpg";
string[] urlParts = url.Split('/');
string file = urlParts[urlParts.length -1];

Comments

0

use Split function with '/' as separator and take 2 last elements of the array.

            string s = "";
            string[] arr = s.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string ans = "";
            if (arr.Length > 1)
                ans = arr[arr.Length - 1] + arr[arr.Length - 2];

Comments

0

I suggest you use the System.Uri class which is tailor-made for this purpose (providing easy access to parts of a URI), e.g.,

Uri uri = new Uri("http://localhost:55164/Images/photos/2/2.jpg");
string[] segments = uri.Segments;
foreach (string segment in segments)
{
    Console.WriteLine(segment.Trim('/'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.