Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap text to the next line when it exceeds a certain length?

I need to write different paragraphs of text within a certain area. For instance, I have drawn a box to the console that looks like this:

/----------------------\
|                      |
|                      |
|                      |
|                      |
\----------------------/

How would I write text within it, but wrap it to the next line if it gets too long?

like image 302
rshea0 Avatar asked May 10 '12 19:05

rshea0


People also ask

How do you break a long text in CSS?

The <wbr> element If you know where you want a long string to break, then it is also possible to insert the HTML <wbr> element. This can be useful in cases such as displaying a long URL on a page.

How do I wrap text in next line in CSS?

The overflow-wrap property in CSS allows you to specify that the browser can break a line of text inside the targeted element onto multiple lines in an otherwise unbreakable place. This helps to avoid an unusually long string of text causing layout problems due to overflow.

How do you wrap a long text in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.


2 Answers

Split on last space before your row length?

int myLimit = 10;
string sentence = "this is a long sentence that needs splitting to fit";
string[] words = sentence.Split(new char[] { ' ' });
IList<string> sentenceParts = new List<string>();
sentenceParts.Add(string.Empty);

int partCounter = 0;

foreach (string word in words)
{
    if ((sentenceParts[partCounter] + word).Length > myLimit)
    {
        partCounter++;
        sentenceParts.Add(string.Empty);
    }

    sentenceParts[partCounter] += word + " ";
}

foreach (string x in sentenceParts)
    Console.WriteLine(x);

UPDATE (the solution above lost the last word in some cases):

int myLimit = 10;
string sentence = "this is a long sentence that needs splitting to fit";
string[] words = sentence.Split(' ');

StringBuilder newSentence = new StringBuilder();


string line = "";
foreach (string word in words)
{
    if ((line + word).Length > myLimit)
    {
        newSentence.AppendLine(line);
        line = "";
    }

    line += string.Format("{0} ", word);
}

if (line.Length > 0)
    newSentence.AppendLine(line);

Console.WriteLine(newSentence.ToString());
like image 54
Jim H Avatar answered Oct 02 '22 22:10

Jim H


I started with Jim H.'s solution and end up with this method. Only problem is if text has any word that longer than limit. But works well.

public static List<string> GetWordGroups(string text, int limit)
{
    var words = text.Split(new string[] { " ", "\r\n", "\n" }, StringSplitOptions.None);

    List<string> wordList = new List<string>();

    string line = "";
    foreach (string word in words)
    {
        if (!string.IsNullOrWhiteSpace(word))
        {
            var newLine = string.Join(" ", line, word).Trim();
            if (newLine.Length >= limit)
            {
                wordList.Add(line);
                line = word;
            }
            else
            {
                line = newLine;
            }
        }
    }

    if (line.Length > 0)
        wordList.Add(line);

    return wordList;
}
like image 4
Mitat Koyuncu Avatar answered Oct 02 '22 22:10

Mitat Koyuncu