Tim Hibbard

Software Architect for EnGraph software


News





Add to Google



My Stats

  • Posts - 593
  • Comments - 337
  • Trackbacks - 507

Twitter












Tag Cloud


Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


Image Galleries


EnGraph Blogs


Links


Other


Roll


January 2008 Entries

Extension Method - String.ToFixedLength


This very simple extension method makes a string a fixed length.  It appends whitespace to a string that is shorter than required or strips characters to a string to is longer than requested.

See ScottGu blog post for a brush up (or intro) on extension methods.

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ParaPlan.Extensions { public static class StringHelper { public static string ToFixedLength(this string s, int width) { string rv = s; if (s.Length > width) { rv = s.Remove(width); } else { int neededWhiteSpace = width - s.Length; rv = s + new string(char.Parse(" "), neededWhiteSpace); } return rv; } } }

 

 

Technorati tags: , ,

posted @ Tuesday, January 29, 2008 11:09 AM | Feedback (2) | Filed Under [ .NET ]