如何通过拆分方法保存路径值 [英] how to save path value by split method
问题描述
如何通过拆分方法仅保存路径的最后一个值
例如:D:/Folder1/abc
或
D:/文件夹1/文件夹2/abc
我只需要将"abc"保存为字符串格式的值,其中"abc"是我的项目名称.
请帮我怎么做
我尝试过这种方法
字符串str1 ="D:/Folder1/abc"
string [] split = str1.Split(''/'',``:'');
how can i save only the last value of the path by split method
For eg: D:/Folder1/abc
OR
D:/Folder1/Folder2/abc
i need to save only "abc" as my value in string format where "abc" is my project name.
please help me how can i do this
i have try this method
string str1= "D:/Folder1/abc"
string[] split = str1.Split(''/'', '':'');
for (int i = 0; i<split.Length; i++)
{
Response.Write(split[i].ToString());//Check here for getting the value..
}
但是它不起作用,我如何仅保存路径的最后一个值?
but its not working how can i save only the last value of the path
推荐答案
使用System.IO.Path.GetFileName.它获取字符串的最后一部分,并且不知道它是文件名还是序列中的最后一个文件夹名称.
Use System.IO.Path.GetFileName. It gets the last part of the string, and does not know if it was a filename, or the last folder name in a sequence.
您好,
这是一种实现方法:
Hello,
Here is a way to do it:
string str1= "D:/Folder1/abc";
string[] split = str1.Split("/");
string lastBit = split[split.Length-1];
Response.Write(lastBit);
但是我认为您可能使用了错误的路径分隔符.即"\"而不是"/".
您的代码是:
But I think you may have your path separator the wrong way round. ie "\" not "/".
The your code is:
string str1 = @"D:\Folder1\abc";
string[] split = str1.Split(Path.DirectorySeparatorChar);
string lastBit = split[split.Length - 1];
Response.Write(lastBit);
瓦莱里.
Valery.
这篇关于如何通过拆分方法保存路径值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!