简体   繁体   中英

split last section of a string in c#

i have a kind of input in my program witch users type a link like " http://www.example.com/dd/sa/...../sample.png " and i looking for a code to give me the last section of the link (sample.png)

More accurate:

user type this in a textbox : " http://www.example.com/dd/sa/...../sample.png "

and i receive this : string a = "sample.png"

in other hand i want know all elements after last "/" in my program. i think we can use split() or trim() but i don't know how!

You can use the Path.GetFileName(String) method

var filename = Path.GetFileName("http://www.example.com/dd/sa/whatever/sample.png");

or

Uri uri = new Uri("http://www.example.com/dd/sa/whatever/sample.png");
var name = uri.Segments[uri.Segments.Length - 1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM