0

假设我有一个字符串

        string[] _strFile;

        foreach (ListViewItem item in listview1.Items)
        {
            string _strRecFileName = item.SubItems[5].Text;
            _strFile = _strRecFileName.Split('\\');
        }

在我的列表视图中,我有一个字符串为 \123\abc\hello\.net\****winxp**** 现在我想获取字符串的最后一个值,即在这种情况下为winxp .. 函数是什么去做?

我可以使用 getupperbond 函数来计算字符串的上限以及如何使用它吗?

4

6 回答 6

2
string[] files = _strRecFileName.Split('\\');
string lastElement = files[files.Length - 1];

当然,如果您正在处理实际的文件名和路径等内容,则使用Path该类可能更容易:

string fileName = Path.GetFileName(_strRecFileName);
于 2010-03-16T06:12:30.957 回答
2

为什么不使用

string expectedPart = Path.GetFileName(item.SubItems[5].Text);

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

于 2010-03-16T06:15:06.477 回答
1

试试这个:

string s = @"a\b\c\d\e";
int index = s.LastIndexOf('\\');
string fileName = s.Substring(index + 1);

在这种情况下,您将只创建一个额外的字符串,因此它将比字符串数组使用更少的内存。但是正如codeka所说,如果文本是正确的路径,那么Path课程会更好。

于 2010-03-16T06:17:52.450 回答
0
String str = "\\123\\abc\\hello\\.net\\winxp";
String[] tokens = str.Split('\\');
String lastToken = tokens[tokens.Length - 1];
于 2010-03-16T06:14:47.683 回答
0
String lastArrayItem = _strFile[_strFile.Length-1];
于 2010-03-16T06:18:27.800 回答
0

你总是可以使用正则表达式

string sub = System.Text.Regularexpressions.Regex.Match(TextVar,@"\\(\w+?)$").groups[1].value
于 2010-03-16T06:21:03.720 回答