c# - vb 正确的函数转换为 c#

标签 c# vb.net substring

尝试将此代码片段转换为 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/

相关文章:

vb.net - Uncaught SyntaxError : Unexpected token } vb.net

vb.net - 如何使 VB.NET 函数的参数作为泛型类型?

c# - 在字符串列表中查找子字符串

c# - 如何在任何文化中使用 MediaLibrary 获取相机胶卷图片

c# null for System.drawing.PointT

c# - 任何 CPU - 首选 32 位

excel - 如何检查子字符串是否在Excel工作表行中的任何条目中?

c# - 显示 MySql 数据的表,用户可以编辑该数据

c# - 什么应该进入顶级命名空间?

objective-c - Objective-C : Get Substring between Double Quotes