CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 25, 2006

Split String Into Lines function in .NET

public static string[] SplitStringIntoLines(string input, 
int maxCharsPerLine)
{
string[] words = input.Split(" ".ToCharArray());
ArrayList lines = new ArrayList();
string curLine = "";

foreach (string w in words)
{
if ((w.Length + curLine.Length + 1) <= maxCharsPerLine)
{
curLine += " " + w;
}
else
{
lines.Add(curLine.Trim());
curLine = w;
}
}
lines.Add(curLine.Trim());
return (string[])lines.ToArray(typeof(string));
}

Post a Comment