从不同长度的字符串中获取特定长度的字符串

时间:2018-08-11 04:38:10

标签: c# .net substring

我已允许用户在字符串中输入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个字符的字符串并将其添加到列表中。请告诉我这里怎么了?或如何实现。

3 个答案:

答案 0 :(得分:2)

您的代码有几个问题:

1)Substring方法的签名是:

public string Substring(int startIndex, int length)

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

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

public string Substring(int startIndex)

这比必须计算出您正在做错的字符串的长度要容易得多,因为promotionBody.Length - 1超出了startIndex的字符串的长度大于零,您将获得异常。

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;
}

答案 1 :(得分:0)

我把//Below in the picture //100 represents 0x7ffeedf75a38 address //200 represents 0x7ffeedf75a34 address //300 represents 0x7ffeedf75a28 address int *p = &a p a +---+ +---+ |100|---->| 1 | +---+ +---+ int **pp = &p; pp p a +---+ +---+ +---+ |300|---->|100|---->| 1 | +---+ +---+ +---+ *pp = &b; pp p b +---+ +---+ +---+ |300|---->|200|---->| 2 | +---+ +---+ +---+ ^^^^^ ^^^^^ 方法的length参数当作结束索引是错误的。它不是结束索引,而是要提取的字符串的长度。现在,我找到了更好的解决方案:

Substring(index,length)

此循环捕获100个字符串,当剩余字符少于100个时引发异常。然后将剩余字符添加到列表中。

答案 2 :(得分:0)

尝试这样的事情:

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));
        }
    }

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