【问题标题】:Substring file path子字符串文件路径
【发布时间】: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


【解决方案1】:

C# 中的字符串是不可变的(一旦创建字符串就无法修改),这意味着当您设置 string cutpath = filepath 时,您将 cutpath 的值设置为 path.Value,因为您尚未分配该值filepath.SubString(63) 的任何东西。修复此更改

string filePath = path.Value;
filePath.Substring(63); // here is the problem
string cutPath = filePath;

string filePath = path.Value;
string cutPath = filePath.Substring(63);

【讨论】:

    【解决方案2】:

    第一:

    // It's do nothing
    filePath.Substring(63);
    
    // Change to this
    filePath = filePath.Substring(63);
    

    第二:

    63 是我要从文件路径中为每个 条目如下所示:/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1 最后一点是我想要的真实信息 显示的是:/DataModifier.cs

    使用 63 是个坏主意。最好找到最后一个“/”并将其保存到某个变量的位置。

    【讨论】:

      【解决方案3】:

      感谢大家的帮助

      我的代码现在可以工作了,看起来像这样:

      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;
      
                      if (filePath.Length > 63)
                      {
                          string cutPath = filePath.Substring(63);
                          if (dictionary.ContainsKey(cutPath))
                          {
                              dictionary[cutPath]++;
                          }
                          else
                          {
                              dictionary.Add(cutPath, 1);
                          }
                      }
                  }
              }
              return dictionary;
          }
      

      【讨论】:

        【解决方案4】:

        读取您的 cmets,您似乎只需要文件路径中的文件名。有一个内置实用程序可以在任何路径上实现这一点。

        来自 MSDN:

        Path.GetFileName

        返回指定路径字符串的文件名和扩展名。

        https://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.110%29.aspx

        这是一个帮助您入门的代码示例。

        string path = @"/GEM4/trunk/src/Tools/TaxMarkerUpdateTool/Tax Marker Ripper v1/Help_Document.docx";
        
        string filename = System.IO.Path.GetFilename(path);
        
        Console.WriteLine(filename);
        

        输出

        Help_Document.docx

        这是你修改后的代码;

        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;
        
                        // extract the file name from the path
                        string cutPath = System.IO.Path.GetFilename(filePath);
        
                        if (dictionary.ContainsKey(cutPath))
                        {
                            dictionary[cutPath]++;
                        }
                        else
                        {
                            dictionary.Add(cutPath, 1);
                        }
                    }
                }
                return dictionary;
            }
        

        【讨论】:

          猜你喜欢
          • 2017-04-24
          • 1970-01-01
          • 2015-02-15
          • 2013-01-08
          • 2018-03-08
          • 2015-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多