1: using System;
2: using System.Collections.Generic;
3:
4: /// <summary>
5: /// Summary description for StringExtention
6: /// </summary>
7: public class StringExtention
8: {
9: /// <summary>
10: /// Returns a list of strings no larger than the max length sent in.
11: /// </summary>
12: /// <remarks>useful function used to wrap string text for reporting.</remarks>
13: /// <param name="text">Text to be wrapped into of List of Strings</param>
14: /// <param name="maxLength">Max length you want each line to be.</param>
15: /// <returns>List of Strings</returns>
16: public static List<String> Wrap(string text, int maxLength)
17: {
18:
19: // Return empty list of strings if the text was empty
20: if (text.Length == 0) return new List<string>();
21:
22: var words = text.Split(' ');
23: var lines = new List<string>();
24: var currentLine = "";
25:
26: foreach (var currentWord in words)
27: {
28:
29: if ((currentLine.Length > maxLength) ||
30: ((currentLine.Length + currentWord.Length) > maxLength))
31: {
32: lines.Add(currentLine);
33: currentLine = "";
34: }
35:
36: if (currentLine.Length > 0)
37: currentLine += " " + currentWord;
38: else
39: currentLine += currentWord;
40:
41: }
42:
43: if (currentLine.Length > 0)
44: lines.Add(currentLine);
45:
46:
47: return lines;
48: }
49:
50:
51: }