【问题标题】:'Strings' does not exist in the current context当前上下文中不存在“字符串”
【发布时间】:2014-02-26 19:28:27
【问题描述】:

我将以下函数从 vb.net 转换为 c#,但我无法弄清楚。

错误 4 当前上下文中不存在名称“字符串”

public string GetBetween(string StringText)
    {
        string functionReturnValue = null;

        string TMP = null;
        string FromS = null;
        string ToS = null;
        FromS = "<Modulus>";
        ToS = "</Modulus>";

        TMP = Strings.Mid(StringText, Strings.InStr(StringText, FromS) + Strings.Len(FromS), Strings.Len(StringText));
        TMP = Strings.Left(TMP, Strings.InStr(TMP, ToS) - 1);

        functionReturnValue = TMP;

        return functionReturnValue;

    }

【问题讨论】:

    标签: c#-4.0


    【解决方案1】:

    Strings 是一个 VB.net 类。如果您希望能够使用它,则必须引用 Microsoft.VisualBasic.dll 程序集并使用 Microsoft.VisualBasic 命名空间。

    如果您尽可能避免使用 VB.net 方法会更好。

    public string GetBetween(string str, string start = "<Modulus>", string end = "</Modulus>")
    {
        var startIndex = str.IndexOf(start);
        var endIndex = str.LastIndexOf(end);
        if (startIndex == -1 || endIndex == -1 || startIndex > endIndex)
            return str;
        return str.Substring(startIndex + start.Length,
                             str.Length - start.Length - end.Length);
    }
    

    【讨论】:

      【解决方案2】:

      在标题中添加using Microsoft.VisualBasic;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2019-08-05
        • 2020-07-31
        • 2021-01-16
        • 1970-01-01
        • 2018-06-07
        相关资源
        最近更新 更多