如何在指定索引处检索值. [英] how to retrieve value at the specified index.
本文介绍了如何在指定索引处检索值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名称为Bank_Transactions 2011-07-17_ 2 .txt
的文件
我正在尝试从文件名的datepart之后获取2(粗体).
我已经试过了:
file.Name.LastIndexOfAny(new char [] {``_''})+ 1
我可以获取其索引,但我想获取该索引的值.
Hi,
I have a file with the name Bank_Transactions 2011-07-17_2.txt
I am trying to get 2(in bold) after datepart from the file name.
I have tried this:
file.Name.LastIndexOfAny(new char[] { ''_'' }) + 1
I am able to get its index but i want to get the value at that index.
Thaks in advance.
推荐答案
^ ]
// first, get the name of the file without the extsension
string filename = System.IO.Path.GetFileNameWithoutExtension(@"c:\somePath\x_y 1-2-3_2.txt");
// you should now have "x_y 1-2-3_2" as the file name
// find the last underscore
int pos = filename.LastIndexOf("_");
// retrieve all of the data after the underscore
string part = "";
if (pos >= 0 && pos <= filename.Length -1)
{
part = filename.Substring(pos);
}
// parse it into an integer
int value;
if (Int32.TryParse(part, out value))
{
// do something with value...
}
else
{
// error
}
从此处开始 [
Start here[^]. Given the position you can use substring to extract the data of interest.
这篇关于如何在指定索引处检索值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文