Skip to main content
EDIT: changed function to deal with words > rowLenght
Source Link
NotPro
  • 652
  • 3
  • 4

Your code is complicated but I think it's can be done easier. I prefer to take text, split it in words and then construct new lines.

public static string SplitLineToMultiline(string input, int rowLength)
{        
    StringBuilder result = new StringBuilder();
    StringBuilder line = new StringBuilder();

    foreachStack<string> (varstack word= innew Stack<string>(input.Split(' '));

    while ( stack.Count > 0 )
    {
        var word = stack.Pop();
        if ( word.Length > rowLength )
        {
            string head = word.Substring(0, rowLength);
            string tail = word.Substring(rowLength);

            word = head;
            stack.Push(tail);
        }

        if ( line.Length + word.Length > rowLength)
        {
            result.AppendLine(line.ToString());
            line.Clear();
        }

        line.Append(word + " ");
    }

    result.Append(line);
    return result.ToString();
}

Your code is complicated but I think it's can be done easier. I prefer to take text, split it in words and then construct new lines.

public static string SplitLineToMultiline(string input, int rowLength)
{        
    StringBuilder result = new StringBuilder();
    StringBuilder line = new StringBuilder();

    foreach (var word in input.Split(' '))
    {
        if ( line.Length + word.Length > rowLength)
        {
            result.AppendLine(line.ToString());
            line.Clear();
        }

        line.Append(word + " ");
    }

    result.Append(line);
    return result.ToString();
}

Your code is complicated but I think it's can be done easier. I prefer to take text, split it in words and then construct new lines.

public static string SplitLineToMultiline(string input, int rowLength)
{        
    StringBuilder result = new StringBuilder();
    StringBuilder line = new StringBuilder();

    Stack<string> stack = new Stack<string>(input.Split(' '));

    while ( stack.Count > 0 )
    {
        var word = stack.Pop();
        if ( word.Length > rowLength )
        {
            string head = word.Substring(0, rowLength);
            string tail = word.Substring(rowLength);

            word = head;
            stack.Push(tail);
        }

        if ( line.Length + word.Length > rowLength)
        {
            result.AppendLine(line.ToString());
            line.Clear();
        }

        line.Append(word + " ");
    }

    result.Append(line);
    return result.ToString();
}
Source Link
NotPro
  • 652
  • 3
  • 4

Your code is complicated but I think it's can be done easier. I prefer to take text, split it in words and then construct new lines.

public static string SplitLineToMultiline(string input, int rowLength)
{        
    StringBuilder result = new StringBuilder();
    StringBuilder line = new StringBuilder();

    foreach (var word in input.Split(' '))
    {
        if ( line.Length + word.Length > rowLength)
        {
            result.AppendLine(line.ToString());
            line.Clear();
        }

        line.Append(word + " ");
    }

    result.Append(line);
    return result.ToString();
}