substring out of range

限于喜欢 提交于 2020-01-30 10:36:09

问题


I am trying to extract the number out of the last part of the string, I have wrote a function to do this but am having problems with out of range index.

Here is the string

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" 

and here is my function

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;
}

I want to be able to get 1.3.6.1.4.1.26928.1.1.1.2.1.2.1.3 this part of the string. I have stepped through the code, the string length is 99 however when AND MY startPos becomes 64 and endPos becomes 98 which is actually within the range.


回答1:


The second argument to Substring(int, int) isn't the "end position", but the length of the substring to return.

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



回答2:


Read the documentation again, the second value is the length, not the index.

As found on MSDN:

public string Substring(
    int startIndex,
    int length
)



回答3:


A different approach to this problem could be through using string.Split() to take care of the parsing for you. The only reason why I would propose this (other than that I like to present additional options to what's already there, plus this is the lazy man's way out) is that from a code perspective, the code is easier IMHO to decompose, and when decomposed, is easier to comprehend by others.

Here's the sample program with some comments to illustrate my point (tested, btw).

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;
    }
}


来源:https://stackoverflow.com/questions/30666755/substring-out-of-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!