【问题标题】:Substring Function not works on .net cf?子字符串函数不适用于.net cf?
【发布时间】:2011-04-14 23:43:42
【问题描述】:

网络CF

我需要使用子字符串来捕获字符串的中间部分。

txtpart.Text = "ab12345678cde";
string item = txtpart.Text.Substring(2,8);

输出应该是 12345678。

但它总是抛出这个异常

System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。

除了子串还有其他功能吗?

【问题讨论】:

  • 你的意思是(2,8)"ab12345678cde".Substring(3,8)"2345678c"

标签: c# compact-framework


【解决方案1】:

您的代码看起来不错。

查看 Daniel Moth 关于 .net cf 上的子字符串问题的博客

The Moth Substring Bug on .net cf

最好的问候

【讨论】:

    【解决方案2】:

    您的示例应该返回2345678c 而不是12345678,但这没关系。您拥有的代码是有效的,不应引发错误。您是否只是为了示例而传递ab12345678cde?你能告诉我们错误发生时真正传入的是什么吗?

    您可以在这里查看您的代码是如何执行的:

    http://ideone.com/jufd3

    如果你的长度太长,我相信你会得到一个错误,但不是你看到的那个。

    【讨论】:

      【解决方案3】:

      我知道这是一个老问题,但这可以帮助某人......

      这是一个简单的函数,您可以使用它来“获取”任何字符串的中间:

      public static string getBetween(string strSource, string strStart, string strEnd)
          {
              int Start, End;
              if (strSource.Contains(strStart) && strSource.Contains(strEnd))
              {
                  Start = strSource.IndexOf(strStart, 0) + strStart.Length;
                  End = strSource.IndexOf(strEnd, Start);
                  return strSource.Substring(Start, End - Start);
              }
              else
              {
                  return "";
              }
          }
      

      要使用它,你调用:

      string mySelectedText = getBetween(txtpart.Text, "b", "c");
      Debug.WriteLine(mySelectedText);
      

      结果将是 12345678

      希望对您有所帮助。问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 2015-10-09
        • 1970-01-01
        相关资源
        最近更新 更多