【问题标题】:StartIndex cannot be less than zero. - Error when trying to change a stringStartIndex 不能小于零。 - 尝试更改字符串时出错
【发布时间】:2013-06-24 18:06:51
【问题描述】:

我有以下 C# 代码:

ArticleContent = ds1.Tables[0].Rows[i]["ArticleContent"].ToString();

if (ArticleContent.Length > 260)
{
   ArticleContent = ArticleContent.Remove(ArticleContent.IndexOf('.', 250)) + "...";
}

这里的问题是我收到此错误消息:

StartIndex 不能小于零。

为什么以及如何解决?

【问题讨论】:

  • 提示:如果您的ArticleContent 字符串中没有. 怎么办??
  • 更好地搜索执行您正在尝试执行的现有功能。你可以想象,你不是第一个写它的人。

标签: c# .net string


【解决方案1】:

您收到该错误是因为索引 250 上或之后没有 '.' 字符,因此 IndexOf 返回 -1。然后,您尝试删除位置 -1 处的字符,这会给您带来您所看到的错误。

还要意识到Remove 仅删除该位置的一个字符,而不是该位置之后的所有内容。我怀疑你想要的是:

if (ArticleContent.Length > 260)
{
   int lastPeriod = ArticleContent.LastIndexOf('.');
   if(lastPeriod < 0)
      lastPeriod = 257;  // just replace the last three characters
   ArticleContent = ArticleContent.Substring(0,lastPeriod) + "...";
}

这将在字符串中添加省略号,确保它不再是 260 个字符,并在可能的情况下中断一个句子。

【讨论】:

  • 不...这意味着没有'。' 250 年后。
【解决方案2】:

很明显为什么它失败了,但你到底想做什么?如果只是将字符串截断到特定长度并指示截断,我可能会建议下面列出的扩展方法。它的用法很简单:

ArticleContent = ArticleContent.Truncate(250);

截断扩展方法:

public static string Truncate(this string pThis, int pLength)
{
    if (string.IsNullOrEmpty(pThis))
        return pThis;

    if (0 >= pLength)
        return string.Empty;

    var lTruncatedString = pThis;
    const string lEllipses = @"…";

    if (pThis.Length > pLength)
    {
        var lSubstringLength = Math.Max(pLength - lEllipses.Length, 0);
        lTruncatedString = pThis.Substring(0, lSubstringLength) + lEllipses;
        if (lTruncatedString.Length > pLength)
            lTruncatedString = lTruncatedString.Substring(0, pLength);
    }

    return lTruncatedString;
}

我希望这会有所帮助。

【讨论】:

  • 欢迎来到 SO。 +1 的好建议。 +1 用于您用户名中的星球大战参考。 -1 用于使用匈牙利符号。
  • 我对您的帖子进行了编辑。请阅读this post on meta
【解决方案3】:

如果下面找不到'.'它将返回 -1,这对于 RemoveAt 无效

ArticleContent.IndexOf('.', 250)

【讨论】:

    【解决方案4】:

    正如其他人所写 - 当您的 ArticleContent 没有“。”时字符 - 方法 .Remove() 将返回 -1。

    我建议在您的if 中再添加一个条件:

    if (ArticleContent.Length > 260 && ArticleContent.Contains('.'))
    {
        ArticleContent = ArticleContent.Remove(ArticleContent.IndexOf('.', 250)) + "...";
    }
    

    【讨论】:

      【解决方案5】:

      有可能没有 .在位置 250 之后。您需要先检查:

      ArticleContent = ds1.Tables[0].Rows[i]["ArticleContent"].ToString();
      
      var periodPosition = ArticleContent.IndexOf('.', 250);
      if (ArticleContent.Length > 260 && periodPosition >= 0)
      {
         ArticleContent = ArticleContent.Remove(ArticleContent.IndexOf('.', 250)) + "...";
      }
      

      【讨论】:

        【解决方案6】:
        ArticleContent = ds1.Tables[0].Rows[i]["ArticleContent"].ToString();
        if (ArticleContent.Length > 260)
        {
            if (ArticleContent.Substring(250).Contains("."))
            {
                ArticleContent = ArticleContent.Remove(ArticleContent.IndexOf('.', 250)) + "...";
            }
            else
            {
                ArticleContent = ArticleContent.Remove(ArticleContent.Substring(0, 250)) + "...";
            }
        }
        

        【讨论】:

          【解决方案7】:

          错误来源:'.' 不会出现在索引 250 之后。在这种情况下,IndexOf 方法返回 -1。

          虽然其他人刚刚确定了错误的根源,但我也会针对您的问题发布修复程序。

          解决方法:使用LastIndexOf方法:

          if (ArticleContent.Length > 260)
          {
             if (ArticleContent.Remove(ArticleContent.LastIndexOf('.') != -1)
             {
                 ArticleContent = String.Concat(ArticleContent.Remove(ArticleContent.LastIndexOf('.')), "...");
             }
             else
             {
                 ArticleContent = String.Concat(ArticleContent.Substring(0, 257), "...")
             }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多