【发布时间】:2015-12-14 04:51:17
【问题描述】:
您好,我正在尝试在文件路径进入字典之前对其进行子串化。我试图声明起点,但出现错误:
StartIndex 不能大于字符串的长度。参数名称:startIndex
这是我的代码
private Dictionary<string,int> CreateDictionary(log logInstance)
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
for (int entryIdex = 0; entryIdex < logInstance.logentry.Count(); entryIdex++)
{
logLogentry entry = logInstance.logentry[entryIdex];
for (int pathIdex = 0; pathIdex < entry.paths.Count(); pathIdex++)
{
logLogentryPath path = entry.paths[pathIdex];
string filePath = path.Value;
filePath.Substring(63);
string cutPath = filePath;
if (dictionary.ContainsKey(cutPath))
{
dictionary[cutPath]++;
}
else
{
dictionary.Add(cutPath, 1);
}
}
}
return dictionary;
}
任何帮助都会很棒。
我也试过了
filePath.Substring(0, 63);
和
filePath.Substring(63, length);
【问题讨论】:
-
你没有将
filePath.Substring(63)返回的字符串赋值给任何变量,为什么?但是,该重载为您提供了第 63 个字符后面的部分,因此字符串必须至少有 63 个字符长。你到底想要什么? -
字符串长度为 81 个字符
-
你能调试它并检查一下吗?该错误似乎表明该字符串少于 63 个字符。另外,我认为这个:
filePath.Substring(63); string cutPath = filePath;需要替换为string curPath = filePath.Substring(63);。 -
@npinit 当我在 filePath 中调试它时,显示的是“/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/Help_Document.docx”
-
幻数 63 代表什么? 实际上你要达到什么目标?
标签: c# dictionary substring