HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Wrapping single line string to multiple lines with specific length

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linewithlengthsinglewrappingmultiplespecificstringlines

Problem

Problem

A single line should be separated by Environment.NewLine so that each row has a length of columns (expect for the last one that may be shorter).

Below is my solution:

public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns  0) sb.Append(Environment.NewLine);
            var index = i*columns;
            var length = Math.Min(columns, singleLineString.Length - index);
            var line = singleLineString.Substring(index, length);
            sb.Append(line);
        }

        return sb.ToString();
    }


Even if the problem is really simple, there are lots of thinkable approaches to solve it. Is there simpler / more elegant solution than the one above?

Solution

I think you shouldn't build a new string there because it's a new responsibility. The method should only split the text and return its parts with the specified length.

The user should decide what to do with the results next. Maybe he does not want to use the Environment.NewLine but some other new-line?

I prefer it to be a split-only method:

public static IEnumerable Split(this string text, int partLength)
{
    if (text == null) { throw new ArgumentNullException("singleLineString"); }

    if (partLength < 1) { throw new ArgumentException("'columns' must be greater than 0."); }

    var partCount = Math.Ceiling((double)text.Length / partLength);
    if (partCount < 2)
    {
        yield return text;
    }

    for (int i = 0; i < partCount; i++)
    {
        var index = i * partLength;
        var lengthLeft = Math.Min(partLength, text.Length - index);
        var line = text.Substring(index, lengthLeft);
        yield return line;
    }
}


I need to complain about the missing {} but I guess you probably know that ;-)

The names of the variables could also be a little bit clearer.

Technically you don't need this if:

if (partCount < 2)
    {
        yield return text;
    }


The result will be the same with out it because the for would execute only once .

Code Snippets

public static IEnumerable<string> Split(this string text, int partLength)
{
    if (text == null) { throw new ArgumentNullException("singleLineString"); }

    if (partLength < 1) { throw new ArgumentException("'columns' must be greater than 0."); }

    var partCount = Math.Ceiling((double)text.Length / partLength);
    if (partCount < 2)
    {
        yield return text;
    }

    for (int i = 0; i < partCount; i++)
    {
        var index = i * partLength;
        var lengthLeft = Math.Min(partLength, text.Length - index);
        var line = text.Substring(index, lengthLeft);
        yield return line;
    }
}
if (partCount < 2)
    {
        yield return text;
    }

Context

StackExchange Code Review Q#141501, answer score: 6

Revisions (0)

No revisions yet.