0

I want to split a string by a certain length, but I also want to make sure that it's a full word. For example, if I had:

string str = "Hello I have a dog ";

and I wanted to split it up into chunks of 5 I would do something like this (which I got from Split String into smaller Strings by length variable):

public  IEnumerable<string> SplitByLength( string s, int length)
{
    for (int i = 0; i < s.Length; i += length)
    {
        if (i + length <= s.Length)
        {
            yield return s.Substring(i, length);
        }
        else
        {
            yield return s.Substring(i);
        }
    }
}

but that would result in output like

"Hello"
"I Hav"
"e a d"
"og"

How could I adapt it so that it would split after 5 or after a whitespace? So I would get:

"Hello"
"I"
"Have"

It's not the best example but it's the best I could come up with off the top of my head. The reason I need it is that I'm displaying the results on a web page, and after a certain amount of words, it gets difficult to read due to a background image, so when it hits the limit it breaks a line but I don't want half of a word on one line and the other half on the next line.

Community
  • 1
  • 1
scseano
  • 21
  • 1
  • 1
  • 2
  • split them after spaces? so where you find a space split – Harry Mar 30 '16 at 10:49
  • 1
    Why not `s.Split(' ');`? – Guy Mar 30 '16 at 10:50
  • 4
    If your issue is textual presentation on a webpage, then you should look into HTML/CSS instead of splitting your text because what happens with different screen sizes etc? – Allan S. Hansen Mar 30 '16 at 10:51
  • Sounds like you're trying to manually wrap text on a webpage. There are plenty of ways to do this already, without splitting strings. As @AllanS.Hansen stated, your best bet is to look into HTML / CSS. There are various libraries as well such as Bootstrap 3. – sr28 Mar 30 '16 at 10:57
  • Your requirement doesn't make sense, if you need to break lines at the end; I would expect that you want to break at the *last* space before a certain string length. But as @Allan wrote, I doubt you should do this programmatically, word wrapping is something that should be done in HTML/CSS. – vgru Mar 30 '16 at 10:57
  • It might be better if you show us a screenshot of what your problem is so we can better understand what you're trying to do. – sr28 Mar 30 '16 at 11:00
  • I was going about it wrong. I'm new to web dev and css/HTML in general so assumed doing it programmatically would be the easiest. Turns out it's about 4 lines of code in a stylesheet. Thanks for re-directing me. – scseano Mar 30 '16 at 12:03

1 Answers1

0

You should use an if/else

                if (i + length <= s.Length)
                {
                    yield return s.Substring(i, length);
                }
                else if(s[i] == ' ')
                {
                    yield return s.Substring(i, s.IndexOf(" ",i) - i);
                }
                else
                {
                    yield return s.Substring(i);
                }
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26