如果长度大于某个值,则拆分字符串

时间:2017-11-17 16:49:55

标签: c# asp.net split itext

我有一个包含注释的标签,我正在使用iTextsharp将这些行插入到pdf中,此处注释的位置是注释框,以及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();

现在的问题是评论可能很长,我想要这样做,所以如果评论的行超过这些许多变种插入下一行并将一半的评论strign到下一行。我试着看看谷歌上的分裂字符串我能找到的所有内容:

    string data = "THExxQUICKxxBROWNxxFOX";

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

这是字符串有xx你拆分它但不起作用的地方。

3 个答案:

答案 0 :(得分:0)

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

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

但是根据你的问题,我想你可能想要递归地返回子串以获得多行< =最大行长度,而不是实际上在特定字符上拆分。

答案 1 :(得分:0)

我将尝试回答这一部分:“...如果评论的行超过这些许多字符,请插入下一行,并将一半的注释字符串带到下一行。”

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

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

答案 2 :(得分:0)

以下是我为您找到的内容我相信您正在寻找的内容如果string Comments.length>##

,您可以将此代码放入if语句中
  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其中一个问题不幸地再也找不到那个问题链接所以不能把链接归功于它

相关问题