James Michael Hare

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

My Links

News

Welcome to my blog! I'm a Sr. Software Development Engineer in Seattle, WA. I've been doing C++/C#/Java development for over 18 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

MCC Logo MVP Logo

Follow BlkRabbitCoder on Twitter

Tag Cloud

Archives

Post Categories

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 ]

Feedback

Gravatar

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

What happens to the feedback?

I posted a comment on the fact a char is not your arbitrary character quite some time ago multiple times, and it never made it live.
9/27/2011 1:22 AM | Deduplicator
Gravatar

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

@Deduplicator: There are automatic filters that tend to filter out feedback that it determines may be spam.

I'm not sure what you mean by "fact a char is not your arbitrary character". Can you explain?
9/27/2011 8:26 PM | James Michael Hare
Gravatar

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

@JMH: Well, those filters should say unequivocally that the contribution is trashed, if it is neither moderated nor simply published. As of last two times (less than a week ago) they said moderates, not rejected out of hand.

I'll certainly try to explain. The problem is that char is 16 Bit, which means a single char can only represent 2**16 values, meaning the characters in the basic multilingual plane. Thats about 1 in 17 Unicode characters, actually somewhat more as the higher planes are still sparsely populated. You need 2 chars and the surrogate pair logic or a UTF-32-char to represent any arbitrary character. And there's no BCL-support for that (as far as I know).
9/28/2011 4:26 AM | Deduplicator
Gravatar

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

@Deduplicator: They go into a spam folder which I check from time-to-time. It's pretty rare that it misfires, so apologies for that.

As far as arbitrary, I think we're kind of picking nits here. The point is that they can pad/trim using a character of their choosing as opposed to just spaces.
9/28/2011 5:43 AM | James Michael Hare
Gravatar

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

A style note:
Instead of
Console.WriteLine("\"" + importantStuff + "\""));

which adds a lot of visual noise to the line, I'd go with something like:

var format=@"result is ""{0}""";
Console.WriteLine(format, importantStuff));

(with the format = line shown only once like the oddData= line)
10/7/2011 11:31 AM | James Curran
Gravatar

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

@James:

That's certainly an option, I tend to prefer concat to format when all i'm doing is joining strings since it's more efficient. This is one of those borderline cases, though, where you could argue surrounding the result in quotes is a form of formatting.

I see your point about the repetition of the line though, though obviously this is just a canned example :-)

10/7/2011 11:58 AM | James Michael Hare
Gravatar

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

Hmm..Nice

Watch EURO 2012 Live Stream https://sites.google.com/site/watcheuro2012livestream/
5/10/2012 1:21 AM | EURO 2012
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: