我正在尝试从字符串的最后部分中提取数字,我已经编写了一个函数来执行此操作,但遇到索引超出范围的问题。
这是字符串
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,但是当 AND MY startPos 变为 64 并且 endPos 变为 98 时,这实际上在范围内。
第二个参数Substring(int, int)
不是“结束位置”,而是要返回的子字符串的长度。
result = property.Substring(startPos, endPos - startPos);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)