【问题标题】:Getting strings of specific length from a string of different length从不同长度的字符串中获取特定长度的字符串
【发布时间】:2018-08-11 04:38:10
【问题描述】:

我允许用户在一个字符串中输入 500 个字符。因为我只能在键/值对中输入 100 个字符。我必须从那个字符串中制作 5 个不同的字符串。也许用户只输入了 50 个字符或 209 个字符。代码如下:

private List<string> MakeCustomDataReadyForPromotion()
{
    var setting = SettingsDbManager.Instance.GetSettingBaseOnTrackId(Settings.PromotionDetailsTrackId);
    var restaurantTitle = setting.PromotionTitle;
    var promotionBody = setting.PromotionBody;
    List<string> message = new List<string>
    {
        restaurantTitle
    };
    if (promotionBody.Length > 400)
    {
        message.Add(promotionBody.Substring(0, 99));
        message.Add(promotionBody.Substring(100, 199));
        message.Add(promotionBody.Substring(200, 299));
        message.Add(promotionBody.Substring(300, 399));
        message.Add(promotionBody.Substring(400, promotionBody.Length - 1));
    }
    else if (promotionBody.Length > 300)
    {
        message.Add(promotionBody.Substring(0, 99));
        message.Add(promotionBody.Substring(100, 199));
        message.Add(promotionBody.Substring(200, 299));
        message.Add(promotionBody.Substring(300, promotionBody.Length - 1));
    }
    else if (promotionBody.Length > 200)
    {
        message.Add(promotionBody.Substring(0, 99));
        message.Add(promotionBody.Substring(100, 199));
        message.Add(promotionBody.Substring(200, promotionBody.Length - 1));
    }
    else if(promotionBody.Length > 100)
    {
        message.Add(promotionBody.Substring(0, 99));
        message.Add(promotionBody.Substring(100, promotionBody.Length - 1));
    }
    else
    {
        message.Add(promotionBody);
    }
    return message;
}

我要做的是获取一个 100 个字符的字符串并将其添加到列表中。请告诉我这里有什么问题?或者我该如何实现。

【问题讨论】:

  • 我建议使用循环来分解它。即继续前进,直到没有人离开。这太笨拙了。
  • 你能分享一个例子吗?

标签: c# .net substring


【解决方案1】:

您的代码有几个问题:

1)Substring方法的签名是:

public string Substring(int startIndex, int length)

所以第二个参数是在您的情况下始终为 100 的长度,而不是您尝试做的 99、199、299 或 399。

2)当您尝试将字符串的其余部分一直到末尾时,请使用不带长度的其他签名:

public string Substring(int startIndex)

这比计算你做错的字符串的长度要容易得多,因为当你的startIndex 大于零时,promotionBody.Length - 1 将超出字符串的长度,你会得到一个例外。

3) 在您提出的答案中,您发现了异常并添加了剩余的字符。对正常和可预测的流程使用异常是非常不受欢迎的,请改用标准流程检查:

private List<string> MakeCustomDataReadyForPromotion() {
    var setting = SettingsDbManager.Instance.GetSettingBaseOnTrackId(Settings.PromotionDetailsTrackId);
    var restaurantTitle = setting.PromotionTitle;
    var promotionBody = setting.PromotionBody;
    int chunkSize = 100;
    List<string> message = new List<string> {
        restaurantTitle
    };
    for (int i = 0; i < promotionBody.Length; i += chunkSize) {
        if (promotionBody.Length - i > chunkSize)
            message.Add(promotionBody.Substring(i, chunkSize));
        else
            message.Add(promotionBody.Substring(i));
    }
    return message;
}

【讨论】:

    【解决方案2】:

    我错误地将Substring(index,length) 方法的length 参数视为结束索引。它不是结束索引,而是您要提取的字符串的长度。现在我找到了更好的解决方案:

    private List<string> MakeCustomDataReadyForPromotion()
    {
        var setting = SettingsDbManager.Instance.GetSettingBaseOnTrackId(Settings.PromotionDetailsTrackId);
        var restaurantTitle = setting.PromotionTitle;
        var promotionBody = setting.PromotionBody;
        var totalCharacters = promotionBody.Length;
        List<string> message = new List<string>
        {
            restaurantTitle
        };
        for (int i = 0; i < totalCharacters; i += 100)
        {
            try
            {
                message.Add(promotionBody.Substring(i, 100));
            }
            catch
            {
                message.Add(promotionBody.Substring(i, totalCharacters - i));
            }
        }
        return message;
    }
    

    此循环捕获100个字符串,当剩余字符小于100时抛出异常。然后我将剩余的字符添加到列表中。

    【讨论】:

      【解决方案3】:

      试试这样的:

      private List<string> MakeCustomDataReadyForPromotion()
          {
              // etc etc
              /// etc etc
      
              AddMessage(promotionBody, message);
          }
      
          private static void AddMessage(AddInCorrectObjectHere promotionBody, List<string> message)
          {
              for (int i = 0; i < Math.Ceiling((decimal)promotionBody.Length / 100); i++)
              {
                  message.Add(promotionBody.Substring(i, (i + 1) * 100 - 1));
              }
          }
      

      警告:未经测试的代码。请确保即使子字符串在 0 到 99 之间也能正常工作,即使消息长度小于 99。

      【讨论】:

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