C# Iterate over string question

Hello all,

I have a string where I need to take only up to 30 characters then after that create a new line.   It needs to also not split in between a word.   How can I do this until the end of the string?
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
How about this:
static class Extensions
{
	public static string WrapText(this string source, int length)
	{
		var count = 0;
		var lines = source.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
			.GroupBy(word => (count += word.Length + 1) / length)
			.Select(group => string.Join(" ", group.ToArray()));

		return string.Join("\n", lines.ToArray());
	}
}

Open in new window

Example usage -
using System;
using System.Linq;

namespace EE_Q28700647
{
	class Program
	{
		static void Main(string[] args)
		{
			var longstring = "Lorem ipsum dolor sit amet, odio etiam natoque aliquet nostra, mauris suspendisse, ad duis, faucibus accumsan. Tortor massa suspendisse pharetra quis imperdiet. Consectetuer eu massa metus pharetra massa pulvinar, nec maecenas lorem ultrices, tortor nec ornare platea nam, nunc tortor. Odio sed consequatur et vehicula pellentesque, vel ipsum, sit in ipsum dignissim neque, tempor ipsum diam, nascetur rhoncus duis venenatis a. Libero mauris. Id etiam suspendisse. Id congue in enim adipiscing sit morbi, sit a, id in. Habitasse nulla, nunc sollicitudin commodo id eleifend tristique, justo quis fames, vehicula hic mi. Sapien ut aptent consectetuer.";
			Console.WriteLine(longstring.WrapText(30));
			Console.ReadLine();
		}
	}

	static class Extensions
	{
		public static string WrapText(this string source, int length)
		{
			var count = 0;
			var lines = source.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
				.GroupBy(word => (count += word.Length + 1) / length)
				.Select(group => string.Join(" ", group.ToArray()));

			return string.Join("\n", lines.ToArray());
		}
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author Commented:
This was perfect thanks a lot.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.