James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 166 , comments - 1431 , trackbacks - 0

My Links

News

Welcome to my blog! I'm a Sr. Software Development Engineer in the Seattle area, who has been performing C++/C#/Java development for over 20 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

Follow BlkRabbitCoder on Twitter

Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

.NET

CSharp

Little Wonders

Little Wonders

vNext

C#/.NET Little Wonders: String Padding and Trimming - Not Just for Spaces!

Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders post can be found here.

This post continues a series of Little Wonders in the BCL String class.  Yes, we all work with strings in .NET daily, so perhaps you already know most of these.  However, there are a lot of little fun things that the String class can do that often get overlooked.

So I’m going to wrap up my little side excursion into the String class with just a quick post on two features we’ve probably all used from time to time, but with a little twist: padding and trimming.

Yes, we’ve probably all padded strings with spaces and numbers with zeros before, or trimmed off extra spaces, but did you know these methods aren’t just confined to whitespace?

Trimming Any Character(s)

If you deal with any user input in your programs, you’ve probably found the joys of the Trim() method already.  All Trim() methods trim the specified type of characters from either the start, end, or both sides of a string until a non-trim char is found.  It comes in the following flavors:

  • Trim()
    • Trims whitespace from front and back of a string.
  • Trim(params char[])
    • Trims all occurrences of the specified characters from front and back of a string.  If the parms array is empty or null, whitespace is trimmed.
  • TrimStart(params char[])
    • Trims all occurrences of the specified characters from the front of a string.  If the params array is empty or null, whitespace is trimmed.
  • TrimEnd(params char [])
    • Trims all occurrences of the specified characters from the back of the string.  If the params array is empty or null, whitespace is trimmed.

So obviously, this can be used to trim space as you’ve probably already done:

   1: var data = "     This is a string retrieved from a data source   ";
   2:  
   3: // result is: "This is a string retrieved from a data source   "
   4: Console.WriteLine("\"" + data.TrimStart() + "\""));
   5:  
   6: // result is: "     This is a string retrieved from a data source"
   7: Console.WriteLine("\"" + data.TrimEnd() + "\""));
   8:  
   9: // result is: "This is a string retrieved from a data source"
  10: Console.WriteLine("\"" + data.Trim() + "\""));

 

But what if the data you want to trim is not whitespace?  Well, this is where the params char[] array comes in handy.  Let’s say, for example, we’re reading data from a file that is a header or something that is “ruled” by pipes and dots (the pipe showing every 5th column).  Here’s how we could trim that:

   1: var oddData = "|....|....|This is a string of data with odd stuff..|....|....|";
   2:  
   3: // result is: "This is a string of data with odd stuff..|....|....|"
   4: Console.WriteLine("\"" + oddData.TrimStart('.','|') + "\""));
   5:  
   6: // result is: "|....|....|This is a string of data with odd stuff"
   7: Console.WriteLine("\"" + oddData.TrimEnd('.','|') + "\""));
   8:  
   9: // result is: "This is a string of data with odd stuff"
  10: Console.WriteLine("\"" + oddData.Trim('.','|') + "\"");

So, as you can see, Trim() and its derivatives can trim a lot more than just whitespace, which can come in handy when you are wanting to trim  noise characters from input data.

Padding With Any Character

Padding expands a string either on the left or right side by padding it with a given character.  If you don’t specify the character, space is the default.  Padding comes in the following forms:

  • PadLeft(int finalLength)
    • Right-justifies the string by padding it on the left to the specified length using a space character.
  • PadLeft(int finalLength, char padChar)
    • Right-justifies the string by padding it on the left to the specified length using the given pad character.
  • PadRight(int finalLength)
    • Left-justifies the string by padding it on the right to the specified length using a space character.
  • PadRight(int finalLength, char padChar)
    • Left-justifies the string by padding it on the right to the specified length using the given pad character.

Note that if the string is already greater than finalLength, the original string is returned with no padding.  Also note that the PadXxx() methods use a left/right paradigm instead of the start/end paradigm of TrimXxx().

So what happens when you want to pad a string (to left or right justify it), but you don’t want to pad with spaces?  For example, let’s say you’re creating a table of contents (using a fixed-width font) 80 characters wide, and you want to put periods (‘.’) from the section name to the page name.  You can do this with padding:

   1: var section = "Appendix";
   2: var page = "132";
   3:  
   4: // "Appendix........................................................................132"
   5: var tableOfContentsEntry = section.PadRight(75, '.') + page.PadLeft(5, '.');

 

 

 

Of course, there’s other ways to accomplish this effect (such as concatenating a new string of ‘.’ using the char repeating constructor), and you can certainly perform zero and space padding already in String.Format() as well, but this is a handy way to pad any string using an arbitrary character.

Summary

So, I hope you enjoyed this brief post on padding/trimming alternate characters from strings!  It may not be earth shattering information, but it can certainly come in handy in the right situations. 

Next week I’m going to be presenting my Little Wonders and Little Pitfalls series of posts at the Microsoft Office in Overland Park, KS, so my post that week will be something lighter. 

I’ve been thinking next week I’ll open back up my toolbox of utility classes I’ve found useful and share some helpful extension methods for the Task and CancellationToken types in the TPL.

 

Technorati Tags: , , , , , ,

Print | posted on Thursday, September 22, 2011 6:27 PM | Filed Under [ My Blog C# Software .NET Little Wonders ]

Powered by: