C# Remove URL from String

This seems like a really easy one but everything I try doesn't seem to work

say I have the following string:

string myString = "http://www.mysite.com/folder/file.jpg";

How can I process that to remove the URL and just leave "file.jpg" as the string value?

Thanks!

Kris

Answers


You can always use System.IO.Path methods

string myString = "http://www.mysite.com/folder/file.jpg";
string fileName = Path.GetFileName(myString); // file.jpg

If you do want to process more complex URIs you can pass it thought the System.Uri type and grab the AbsolutePath

string myString = "http://www.mysite.com/folder/file.jpg?test=1";
Uri uri = new Uri(myString);
string file = Path.GetFileName(uri.AbsolutePath);

Need Your Help

Compiling to a different iOS version

iphone objective-c sdk ios7 xcode5

I have an application in the App Store, targeted for iOS 5, that runs correctly in iOS 6, and even iOS 7, but now, I wanted to add a new feature (just one new view with a controller).

How to install Joomla 2.5 component by copying the files without using the J install manager

php joomla2.5 jcomponent

When installing custom component on a Joomla 2.5 everything works well. If I just copy and paste the content of the component in the appropriate folder /components/com_mycomponent without installin...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.