logo

A Couple of C# String and Date Helpers

In JavaScript you can extend the available methods of built-in objects like Strings and do stuff like this:

String.prototype.trim = function(){return this.replace(/^\s+|\s+$/, '');};

Which then allows you to do this:

alert(document.forms[0].SomeField.value.trim());

Which I always prefer to the alternative which would look something like this:

alert(trim(document.forms[0].SomeField.value));

Dunno why. I just think it looks more professional. There are probably good reasons to do it this way as well. Say, like, it stops you accidentally making two trim methods which do two different things.

Until today I'd never realised you can do this in "real" programming languages too. Well, at least you can in C#.

Here's a string extension helper method to encrypt a password:

public static class StringExtensions
{
 public static string Encrypt(this string password)
 {
  return BitConverter.ToString(
        SHA1Managed.Create().ComputeHash(
                Encoding.Default.GetBytes(password)
        )).Replace("-", "");
 }
}

The key here is that the class and method are both static and also that we're using the "this" keyword before the string argument we're passing to the method. This tells C# the argument can be passed to it by calling our new method on a string. Like so:

Console.WriteLine("my password".Encrypt());
Console.WriteLine(Request["MyPassword"].Encrypt());

Nice.

I'm now in the process of retro-fitting this approach to a couple of apps I've done recently.

It follows that you can do the same thing for other objects, such as DateTime. Here's a helper method to turn a date in to words -- such as "Three weeks ago":

public static class DateExtensions {
 public static string ToWords(this DateTime dateTime)
 {
   //Code to do this for real is on Google et al
   return "Three weeks ago";
 }
}

The beauty of doing this in C# is that Visual Studio's Intelli-sense picks up on these new extensions and adds them to type-ahead, as you can see below in this Razor-based MVC View:

image

Working with C# and Visual Studio really is a pleasure. It makes learning to be a real programmer after all these years so much easier.

Comments

    • avatar
    • John Vaughan
    • Thu 3 Mar 2011 05:05 PM

    Just starting to get into C# over here. It looks completely awesome to me after only a few days of training. Flexible like Javascript, but solidly OO like Java. Neat stuff for sure. Lots to learn.

      • avatar
      • Jake Howlett
      • Fri 4 Mar 2011 02:49 AM

      "Lots to learn".

      Indeed. I can't imagine I'll ever completely master C#. There's just too much to it. But that's something I like about it. The fact that there's always something new to learn. My coding career (with Domino) had reached (a long time ago!) the point where I had nothing left to learn. Man, was I bored. Switching to C# has given me a fresh outlook on it all.

      I just hope I don't regret that the prime of my career was wasted on Domino. And that it's not too late (at 36) to become a real programmer.

    • avatar
    • CJ
    • Fri 4 Mar 2011 02:04 AM

    Is it a bit weird to have favourite string functions? Well even if it is, one of mine is String.Format which can be used to do placeholders, followed by arguments to fill in those placeholders.

    e.g. Console.WriteLine(String.Format("Today is {0}!", DateTime.Today.DayOfWeek.ToString()));

      • avatar
      • Jake Howlett
      • Fri 4 Mar 2011 02:44 AM

      Not weird at all. We're all geeks here!

      I love String.Format() too and am only scratching at the surface of what it can do.

      It took me a while to get in to the habit of using .Format instead of just using:

      "Today is "+DateDateTime.Today.DayOfWeek.ToString();

      Taking what I wrote above you can of course write:

      string.Format("It was {0} since you last logged in", user.LastLoginTime.ToWords());

      As for favourite function - hmm, dunno what mine is...

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 3 Mar 2011

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content