Split long string at the spaces

.net, c#, string

Solution

Maybe use a regular expression like this:

var input = "This is a long sentence with more than 18 letters.";
var output = Regex.Split(input, @"(.{1,18})(?:\s|$)")
                  .Where(x => x.Length > 0)
                  .ToList();

Returns the result:

[ "This is a long", "sentence with more", "than 18 letters." ]

Update

Here's a similar solution to handle really long words (although I have a feeling it won't perform quite as well, so you may want to benchmark this):

var input = "This is a long sentence with a reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreally long word in it.";
var output = Regex.Split(input, @"(.{1,18})(?:\s|$)|(.{18})")
                  .Where(x => x.Length > 0)
                  .ToList();

This produces the result:

[ "This is a long", 
  "sentence with a", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "reallyreallyreally", 
  "really long word", 
  "in it." ]

Problem

In my program, I need to split a string into multiple lines if it's too long. Right now I'm using this method: ``` private List<string> SliceString(string stringtocut) { List<string> parts = new List<string>(); int i = 0; do { parts.Add(stringtocut.Substring(i, System.Math.Min(18, stringtocut.Substring(i).Length))); i += 18; } while (i < stringtocut.Length); return parts; } ``` The only thing is that if the 19'th character isn't a space, we cut a word in half, and it looks really bad. E.g. String: This is a long sentance with more than 18 letters. ``` Sliced string: This is a long sent ance with more than 18 letters. ``` How would I cut the string so it's no longer than 18 characters per section, but go back to the nearest space if we can? I've been toying with the above algorithm for a bit, but I can't seem to get it. Thanks!

Original source