Skip to main content
Question Protected by Jamal
Tweeted twitter.com/StackCodeReview/status/776805042502459392
Typos
Source Link
forsvarir
  • 11.7k
  • 6
  • 38
  • 71

Problem

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

Below is my solution:

    public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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 aboveeabove?

Problem

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

Below is my solution:

    public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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 abovee?

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 < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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?

added 4 characters in body
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48

Problem

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

Below is my solution:

    public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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 abovee?

Problem

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

Below is my solution:

    public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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 abovee?

Problem

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

Below is my solution:

    public static string Wrap(this string singleLineString, int columns)
    {
        if (singleLineString == null)
            throw new ArgumentNullException("singleLineString");
        if (columns < 1)
            throw new ArgumentException("'columns' must be greater than 0.");
        
        var rows = Math.Ceiling((double)singleLineString.Length/columns);
        if (rows < 2) return singleLineString;

        var sb = new StringBuilder();

        for (int i = 0; i < rows; i++)
        {
            if (i > 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 abovee?

edited tags; edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48
Loading