Bryan Reynolds

Software, Business, Life . . .

Contact

Bryan Reynolds
  Bryan Reynolds Linked In
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

C# Wrapping text using split and List<>

Below is a useful snippet of code to wrap text into a list of strings.  Download Click here.

 

Example

"The quick brown fox jumps over the lazy dog"

 

Lets say you need to fit this into a multi-line text box with a width or around 12 characters and an unlimited height.

 

The function will return a list of strings without splitting the individual words.

 

   1: var result = Wrap("The quick brown fox jumps over the lazy dog", 12);

 

result [0] = "The quick"

result [1] = "brown fox "

result [2] = "jumps over"

result [3] = "the lazy dog"

 

This function was used for reporting writing and formatting logs.

 

The function takes a string of any size and returns a list of string no larger than the max length input variable.

 

Let me know if this was useful.

   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: }

 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: C#
Posted by Bryan on Wednesday, May 07, 2008 11:04 AM
Permalink | Comments (4) | Post RSSRSS comment feed