简体   繁体   English

如何拆分字符串以获取文件名?

[英]How can I split a string to obtain a filename?

I am trying to split the string. 我想分裂字符串。 Here is the string. 这是字符串。

string fileName =   "description/ask_question_file_10.htm"

I have to remove "description/" and ".htm" from this string. 我必须从此字符串中删除“description /”和“.htm”。 So the result I am looking for "ask_question_file_10". 所以结果我正在寻找“ask_question_file_10”。 I have to look for "/" and ".htm" I appreciate any help. 我必须寻找“/”和“.htm”我感谢任何帮助。

You can use the Path.GetFileNameWithoutExtension Method : 您可以使用Path.GetFileNameWithoutExtension方法

string fileName = "description/ask_question_file_10.htm";

string result = Path.GetFileNameWithoutExtension(fileName);
// result == "ask_question_file_10"
string fileName = Path.GetFileNameWithoutExtension("description/ask_question_file_10.htm")

try 尝试

string myResult = fileName.SubString (fileName.IndexOf ("/") + 1);
if ( myResult.EndsWith (".htm" ) )
   myResult = myResult.SubString (0, myResult.Length - 4);

IF it is really a path then you can use 如果它真的是一条路,那么你可以使用

string myResult = Path.GetFileNameWithoutExtension(fileName);

EDIT - relevant links: 编辑 - 相关链接:

 string fileName = "description/ask_question_file_10.htm";
 string name = Path.GetFileNameWithoutExtension(fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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