开发者

How Can i Fetch a Word of a String?

开发者 https://www.devze.com 2023-01-07 03:37 出处:网络
I have the following string: string path = \"C:\\Users\\Username\\Desktop\\FileName.pdf\"; I need to take off FileName.pdf and put in a string variable called fileName.

I have the following string:

string path = "C:\Users\Username\Desktop\FileName.pdf";

I need to take off FileName.pdf and put in a string variable called fileName.

The code has to be generic in a sense that anytime i get path, i start going from the en开发者_如何学运维d of the string down to the first backslash.

I'm using C#


You can use System.IO.Path to manipulate a path.

GetFileName Returns the file name and extension of the specified path string.

For completeness here's the code:

string fileName = Path.GetFileName(path);


Use a System.IO.FileInfo object - you can get the filename using this easily.


Well you can use Split('\') and you get the last of the array

0

精彩评论

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

关注公众号