-3

我一直在努力理解子字符串在 C# 中是如何工作的。如何在 C# 中使用子字符串来删除文件格式?

从 :

测试.xml

至:

测试

4

6 回答 6

3

Although Path.GetFileNameWithoutExtension is the normal way to do it, you can do it with Substring. You first have to find the period.

string name = "test.xml";
int pos = name.LastIndexOf('.');
if (pos >= 0)
{
    name = name.Substring(0, pos);
}
于 2013-08-28T12:20:57.250 回答
3

System.IO.Path.GetFileNameWithoutExtension()为此,您应该使用方法:

Path.GetFileNameWithoutExtension("filename");

如果要使用Substring()方法,则必须使用 找到最后一个点索引LastIndexOf('.'),然后执行Substring(0,lastIndex)(当然如果找到的最后一个索引不是-1

于 2013-08-28T12:18:54.743 回答
1

If you want to use substring:

string file = "test.xml";
string filewithoutextention = file.Substring(0,file.IndexOf('.'));
于 2013-08-28T12:20:49.517 回答
0

您不需要在这里使用 Substring,因为您可以使用System.IO.Path.GetFileNameWithoutExtension.

于 2013-08-28T12:19:23.097 回答
0

System.IO.Path.GetFileNameWithoutExtension应该可以帮助你。

于 2013-08-28T12:19:52.887 回答
0

利用

Path.GetFileNameWithoutExtension("test.xml");
于 2013-08-28T12:19:09.107 回答