尝试将此代码片段转换为 C# 时遇到一些问题
Private Shared Function GetOccInt(ByVal itm As String) As Integer
Dim pos As Integer = InStr(itm, ">")
Dim strTemp As String = Trim(Replace(Right(Trim(itm), itm.Length - pos), "</b>)</a>", String.Empty))
pos = InStr(strTemp, ">")
Return Convert.ToInt32(Trim(Right(strTemp, strTemp.Length - pos)))
End Function
我最后的尝试应该是正确的,但显然不是:
private static int GetOccInt(string itm)
{
var pos = itm.IndexOf(">", StringComparison.Ordinal);
var strTemp = itm.Trim().Substring(itm.Length - pos, itm.Length).Replace("</b>)</a>", "");
pos = strTemp.IndexOf(">", StringComparison.Ordinal);
return Convert.ToInt32(strTemp.Trim().Substring(strTemp.Length - pos, strTemp.Length));
}
调试时,我收到子字符串函数的参数超出范围错误消息 = 索引和长度必须引用字符串中的某个位置。 参数名称:长度
第一次遇到断点时,索引是正确的 (12),itm.Length 显示为 31,它似乎有 31 个字符,但由于 itm.length 是系统代码,不确定如何产生错误。
最佳答案
VB
Right(Trim(itm), itm.Length - pos)
对应C#
itm.Trim().Substring(pos)
关于c# - vb 正确的函数转换为 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053644/