Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split long string at the spaces

Tags:

string

c#

.net

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!

like image 961
Nathan Avatar asked Dec 03 '13 20:12

Nathan


2 Answers

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." ]
like image 51
p.s.w.g Avatar answered Sep 20 '22 17:09

p.s.w.g


This is really not optimized nor elegant code, but gives the desired result. It should be relatively easy to refactor :

string longSentence = "This is a long sentence with more than 18 letters.";

List<string> words = new List<string>();
string currentSentence = string.Empty;

var parts = longSentence.Split(' ');
foreach (var part in parts)
{
    if ((currentSentence + " " + part).Length < 18)
    {
        currentSentence += " " + part;
    }
    else
    {
        words.Add(currentSentence);
        currentSentence = part;
    }
}
words.Add(currentSentence);
words[0] = words[0].TrimStart();

Result :

This is a long
sentence with
more than 18
letters.

Basically you add each words up until you're about to bust 18 letters. At this point, you save the part and start over again. When it's over, you add up what's left. Also, there's an unnecessary space at the start that needs some trimming.

like image 42
Pierre-Luc Pineault Avatar answered Sep 17 '22 17:09

Pierre-Luc Pineault