开发者

C# Remove URL from String

开发者 https://www.devze.com 2023-01-27 12:56 出处:网络
This seems like a really easy one but everything I try doesn\'t seem to work say I have the following 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开发者_StackOverflow the string value?

Thanks!

Kris


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);


string lastPart = myString.Substring(myString.LastIndexOf('/') + 1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号