Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting text into lines with maximum length

I have a long string and I want to fit that in a small field. To achieve that, I break the string into lines on whitespace. The algorithm goes like this:

    public static string BreakLine(string text, int maxCharsInLine)
    {
        int charsInLine = 0;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < text.Length; i++)
        {
            char c = text[i];
            builder.Append(c);
            charsInLine++;

            if (charsInLine >= maxCharsInLine && char.IsWhiteSpace(c))
            {
                builder.AppendLine();
                charsInLine = 0;
            }
        }
        return builder.ToString();
    }

But this breaks when there's a short word, followed by a longer word. "foo howcomputerwork" with a max length of 16 doesn't break, but I want it to. One thought I has was looking forward to see where the next whitespace occurs, but I'm not sure whether that would result in the fewest lines possible.

like image 352
John NoCookies Avatar asked Mar 08 '13 12:03

John NoCookies


2 Answers

Enjoy!

public static string SplitToLines(string text, char[] splitOnCharacters, int maxStringLength)
{
    var sb = new StringBuilder();
    var index = 0;

    while (text.Length > index)
    {
        // start a new line, unless we've just started
        if (index != 0)
            sb.AppendLine();

        // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength`
        var splitAt = index + maxStringLength <= text.Length
            ? text.Substring(index, maxStringLength).LastIndexOfAny(splitOnCharacters)
            : text.Length - index;

        // if can't find split location, take `maxStringLength` characters
        splitAt = (splitAt == -1) ? maxStringLength : splitAt;

        // add result to collection & increment index
        sb.Append(text.Substring(index, splitAt).Trim());
        index += splitAt;
    }

    return sb.ToString();
}

Note that splitOnCharacters and maxStringLength could be saved in user settings area of the app.

like image 94
Dead.Rabit Avatar answered Sep 29 '22 12:09

Dead.Rabit


Check the contents of the character before writing to the string builder and or it with the current count:

    public static string BreakLine(string text, int maxCharsInLine)
    {
        int charsInLine = 0;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < text.Length; i++)
        {
            char c = text[i];
            if (char.IsWhiteSpace(c) || charsInLine >= maxCharsInLine)
            {
                builder.AppendLine();
                charsInLine = 0;
            }
            else 
            {
                builder.Append(c);
                charsInLine++;                    
            }
        }
        return builder.ToString();
    }
like image 31
Moop Avatar answered Sep 29 '22 11:09

Moop