【问题标题】:substring out of range子串超出范围
【发布时间】:2015-06-05 12:36:54
【问题描述】:

我正在尝试从字符串的最后一部分中提取数字,我已经编写了一个函数来执行此操作,但遇到超出范围索引的问题。

这是字符串

type="value" cat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.1" descCat=".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3" 

这是我的功能

private static string ExtractDescOID(string property)
{
    string result = "";
    int startPos = property.LastIndexOf("descOid=\"") + "descOid=\"".Length;
    int endPos = property.Length - 1;
    if (endPos - startPos != 1)
    {
        //This now gets rid of the first . within the string.
        startPos++;
        result = property.Substring(startPos, endPos);
    }
    else
    {
        result = "";
    }

    if (startPos == endPos)
    {
        Console.WriteLine("Something has gone wrong");
    }

    return result;
}

我希望能够得到1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3这部分字符串。我已经通过代码,字符串长度是 99 但是当我的 startPos 变为 64 并且 endPos 变为 98 时,实际上在范围内。

【问题讨论】:

  • Substring() 采用起始索引和长度,64 + 98 > 99
  • 您尝试获取descOid= 的起始索引,但它不是您的字符串的一部分。还是我瞎了?

标签: c# string substring


【解决方案1】:

Substring(int, int) 的第二个参数不是“结束位置”,而是要返回的子字符串的长度。

 result = property.Substring(startPos, endPos - startPos);

【讨论】:

    【解决方案2】:

    再看文档,第二个值是长度,不是索引。

    如发现on MSDN:

    public string Substring(
        int startIndex,
        int length
    )
    

    【讨论】:

      【解决方案3】:

      解决此问题的另一种方法是使用string.Split() 为您处理解析。我提出这个的唯一原因(除了我喜欢为已经存在的东西提供额外的选项,而且这是懒人的出路)是从代码的角度来看,代码更容易在恕我直言,分解时, 更容易被他人理解。

      这是带有一些 cmets 的示例程序来说明我的观点(经过测试,顺便说一句)。

      class Program
      {
          static void Main(string[] args)
          {
              var someAttributesFromAnXmlNodeIGuess = 
      "type=\"value\" cat=\".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.1\" descCat=\".1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3\"";
      
              var descCat = GetMeTheAttrib(someAttributesFromAnXmlNodeIGuess, "descCat");
      
              Console.WriteLine(descCat);
              Console.ReadLine();
          }
      
          // making the slightly huge assumption that you may want to 
          // access other attribs in the string...
          private static string GetMeTheAttrib(string attribLine, string attribName)
          {
              var parsedDictionary = ParseAttributes(attribLine);
      
              if (parsedDictionary.ContainsKey(attribName))
              {
                  return parsedDictionary[attribName];
              }
      
              return string.Empty;
          }
      
          // keeping the contracts simple - 
          // i could have used IDictionary, which might make sense 
          // if this code became LINQ'd one day
          private static Dictionary<string, string> ParseAttributes(string attribLine)
          {
              var dictionaryToReturn = new Dictionary<string, string>();
      
              var listOfPairs = attribLine.Split(' '); // items look like type=value, etc
              foreach (var pair in listOfPairs)
              {
                  var attribList = pair.Split('=');
      
                  // we were expecting a type=value pattern... if this doesn't match then let's ignore it
                  if (attribList.Count() != 2) continue; 
      
                  dictionaryToReturn.Add(attribList[0], attribList[1]);
              }
      
              return dictionaryToReturn;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 2020-10-20
        • 2010-10-31
        • 2023-03-27
        • 1970-01-01
        相关资源
        最近更新 更多