【问题标题】:Split String if length is more than a value如果长度大于一个值,则拆分字符串
【发布时间】:2017-11-17 16:49:54
【问题描述】:

我有一个包含 cmets 的标签,我正在使用 iTextsharp 将这些行插入到 pdf 中,现在 cmets 进入这里的地方是 cmets int he pdf 的框:

This is the PDF comment box

我将使用此代码放入一行:

     string comment = lblComment.text;
// lets say the lblComment.text = "This is a Document where User Added new Address"
         cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment, 215, 96, 0); // Insert the Cooments
            cb.EndText();

现在的问题是评论可能很长,我想要一些方法来做到这一点,所以如果评论的行超过这些许多字符插入下一行并将评论字符串的一半放到下一行。我试着在谷歌上查看所有我能找到的分割字符串:

    string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);

这是字符串有 xx 你拆分它但没有工作的地方。

【问题讨论】:

标签: c# asp.net split itext


【解决方案1】:

这是我为你找到的东西我相信这就是你正在寻找的东西你可以将此代码放在 if 语句中 if string Comments.length>##

  string YourComment = lblComment.text;
    string[] Peaces= a.Split(' ');

    int counter = 0;
    string first = "";
    int Center= a.Length / 2; // Devied it into 2 peaces 
    while (first.Length < Center)
    {
        firstHalf += parts[Peaces] + " ";
        counter++;
    }
    string secondHalf = string.Join(" ", Peaces.Skip(counter)); // Join the string
    string Comment1 = firstHalf ;
    string Comment2 = secondHalf;

现在您可以像这样将这 2 个字符串用于 2 个等长的行

 cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment1, 215, 96, 0); // Insert the Cooments
            cb.EndText();
 cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment2, 215, 96, 0); // Insert the Cooments
            cb.EndText();

我读了这个 Stack over flow 的问题之一,不幸的是找不到那个问题的链接,所以不能把一个链接归功于它

【讨论】:

    【解决方案2】:

    从您的代码中我认为您只是在寻找

    return Regex.Split(data, "xx");
    

    但从您的问题来看,我认为您可能希望递归地返回子字符串以获取多行

    【讨论】:

    • 这不会编译。您不能使用字符串作为参数进行拆分。它只需要字符、字符数组或字符串数​​组。你必须这样做return data.Split(new string[] { "xx" }, StringSplitOptions.None)msdn.microsoft.com/en-us/library/…
    • 是的,我在考虑 Regex.Split,我总是将这两者混为一谈。感谢您了解这一点,已更新答案。
    【解决方案3】:

    我将尝试回答这部分:“......如果评论的行超过这些字符数,则插入下一行并将评论字符串的一半放到下一行。”

    public string Summarize(string input, int length)
    {
        if (input.Length <= length) return input;
        string result = input.Substring(0, length) + System.Environment.NewLine + input.Substring(length, length/2);
        //You may want to add more logic like spliting the string at the previous whitespace
        return result;
    }
    

    edit- 执行此任务有更有效的方法,例如使用 StringBuilder,但我在这里是为了可读性。

    【讨论】:

    • 这种工作但它返回的字符串放在一些随机单词中的问题是这样的ns"
    • \r\n 是回车符和换行符。所以,你得到了“Ha[new line]s”。
    • 当我说换行时,我并不是指那一行,正如你看到的那样,我使用字符串放入 2 行,我的意思是将字母放到另一个字符串中,并将它们放入那 2 行中。跨度>
    • @Haseeb 您能否在原始帖子中放一个评论字符串的示例以及您需要的输出?
    • 当然,让我将其添加到我的问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多