在一定数量的字符和换行符后拆分字符串。在C#中

时间:2016-11-02 15:15:34

标签: c# string substring

后台:我正在使用大字符串(500-5000个字符)并将它们放在PPTX文件中。我的幻灯片只有足够的空间,每张幻灯片大约1000个字符。我希望在幻灯片已满后分割文本并继续下一张幻灯片。据我所知,如果文本已经在幻灯片/文本框之外(我正在使用“OpenXML SDK与”Open-XML PowerTools结合使用“),则无法检查幻灯片(非常肯定)。

到目前为止

进程:我设法编写了一个方法,在maxLength小于1000个characers时,在1000个字符之后将字符串拆分而不会触发错误(因为它发生在Substring(startIndex,maxLength) ))。这是我的截断方法:

public static string Truncate(string text, int startIndex, int maxLength)
        {

            if (string.IsNullOrEmpty(text)) return text;
            if (text.Length > startIndex + maxLength)
            {
                return text.Substring(startIndex, maxLength) + "-";
            }
            return text.Substring(startIndex);
        }

有问题:现在的问题是,某些字符串有很多换行符,而其他字符串很少或没有。这导致一些字符串需要很多高度并且要大到适合幻灯片。

可能的想法:我考虑估算计算换行符的字符串高度,并为每个换行符添加30个字符。这给出了更精确的近似值。 (一行中的字符通常包含大约50个字符,但有时换行位于行的中间,所以我认为+30个字符是一个很好的猜测)。到目前为止,这是我的方法:

public int CountCharacters(string text)
{
    var numLines = text.Length;
    numLines += (text.Split('\n').Length) * 30;
    return numLines;
}

结果问题:我现在如何将这些方法结合起来,在我达到1000并考虑换行符后拆分字符串?或者这毕竟是错误的做法?

3 个答案:

答案 0 :(得分:1)

您可以测量字符串,并确定所需的空间。请记住,字体是一个因素,因此如果您使用多种字体,则需要进行多次计算。

https://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx

答案 1 :(得分:1)

这样的事情怎么样:

        string test; //your input string
        int j;  //variable that holds the slide content length
        int max_lines = 10;  //desired maximum amount of lines per slide
        int max_characters = 1000; //desired maximum amount of characters
        char[] input = test.ToCharArray();  //convert input string into an array of characters
        string slide_content;    //variable that will hold the output of each single slide

        while (input.Length > 0)
        {
            //reset slide content and length
            slide_content = string.Empty;
            j = 0;

            //loop through the input string and get a 'j' amount of characters
            while ((slide_content.Split('\n').Length < max_lines) && (j < max_characters))
            {
                j = j + 1;
                slide_content = new string(input.Take(j).ToArray());
            }

            //Output slide content
            Console.WriteLine(slide_content);
            Console.WriteLine("=================== END OF THE SLIDE =====================");          

            //Remove the previous slide content from the input string
            input = input.Skip(j).ToArray();       
        }

        Console.Read();

答案 2 :(得分:1)

我认为您最好只是手动计算truncate函数中的换行符。那些Split调用并不便宜,而你使用它们只是为了计算换行符。这是我的尝试(CHARS_PER_NEWLINE是一个对应于你建议的30的全局常量):

public static string Truncate(string text, int startIndex, int maxLength)
    {

        if (string.IsNullOrEmpty(text)) return text;
        int remaining = maxLength;
        int index = startIndex;
        while (remaining > 0 && index<text.Length)
        {
            if (text[index] == '\n')
            {
                remaining -= CHARS_PER_NEWLINE;
            }
            else
            {
                remaining--;
            }
            index++;
        }
        return text.Substring(startIndex, index - startIndex) + '-';
    }
相关问题