views:

568

answers:

4

I'm writing a library of Extension Methods for String and DateTime utility functions in C#. Can you please help me out by suggesting the useful utlity functions for String and DateTime you may want to be part of it ? With your suggestions I can make it more cohesive and Collective.

Thanks!

+4  A: 

String Extensions

  1. MakeTitle -- Make a title from a TitleCase string, .i.e, turn "FooBar" in to "Foo Bar". I find this very useful for printing Enums: fooEnum.ToString("g").MakeTitle()
  2. Collapse -- Trim whitespace from both ends and collapse all internal spaces to a single space.
  3. IsNothing -- Like IsNullOrEmpty, but trims whitespace first, helpful for TextBox inputs that you don't want to be just spaces, but set to null if nothing was input.

DateTime Extensions

  1. EndOfDay -- set time to 11:59:59 PM on the given date
  2. StartOfDay -- set time to 12:00:00 AM on the given date
tvanfosson
IsNullOrEqual - do you mean IsNullOrEmpty?
Marc Gravell
Duh -- I got to stop answering before I have my coffee.
tvanfosson
StartOfDay is just DateTime.Date
Joel Coehoorn
I like the symmetry with EndOfDay -- though, it could also be used to give business hours instead of midnight -- same with EndOfDay.
tvanfosson
+7  A: 
public static bool IsNullOrEmpty(this string value){
    return string.IsNullOrEmpty(value);
}
public static string Reverse(this string value) {
    if (!string.IsNullOrEmpty(value)) {
        char[] chars = value.ToCharArray();
        Array.Reverse(chars);
        value = new string(chars);
    }
    return value;
}
public static string ToTitleCase(this string value) {
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
}
public static string ToTitleCaseInvariant(this string value) {
    return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(value);
}

Trivial, but slighty nicer to call.

Marc Gravell
The second one ought to be bool IsEmpty(this string value) {...} as a null object couldn't have an instance call.
Stevo3000
@Stevo3000 - yes it can!! Extension methods can be called on null. Seriously. Try it.
Marc Gravell
you can have a string initialized to null and still have it call the Extension Methods.
this. __curious_geek
@Marc - Fair enough, just doesn't seem logical.
Stevo3000
@Stevo3000 - Why not. Here's the code Example: bool isItThere = Request.QueryString["name"].IsNullOrEmpty();
this. __curious_geek
@Marc - Your function is completely logical, what I was suggesting wasn't logical is that it looks like you are caling an instance method on a possibly null value.
Stevo3000
Purely as a convenience - s.Foo() vs string.Foo(s)
Marc Gravell
I didn't undestand the purpose of the first one, till you guys talked about the extensions call on null value, thanks, nice trick
Jhonny D. Cano -Leftware-
Definatly conveiant, and if we were targeting 3.5 it'd be the first extension I wrote. So are extension methods always valid on null objects? If so that seem confusing.
Stevo3000
@Stevo3000 - always. It is just a compiler trick, that translates obj.SomeMethod() to SomeClass.SomeMethod(obj), and null is perfectly legal as an *argument*. There is no null-check.
Marc Gravell
@Marc - Cheers for clearing that up. I guess it gives conveniance and power as a trade off for slightly confusing code.
Stevo3000
Matt
A: 

String Extensions

static string ToCamelCase(this string s) {...}  // Converts a string into Camel Notation, useful for code generation
static string ToPascalCase(this string s) {...} // Converts a string into Pascal Notation
static int [Soundex][1](this string s) {...}      // Gets the soundex of a string

DateTime Extensions

static bool IsWithinRange(this DateTime d, DateTime start, DateTime end) {...}
static string [ToRelativeTime][2](this DateTime d) {...}
Jhonny D. Cano -Leftware-
+4  A: 

What about methods that don't specifically extend string or DateTime, but rather target or return a string or DateTime? Then you could build some int and TimeSpan methods as well, so you can write fluent interfaces like:

  DateTime yesterday =  1.Days().Ago();

.

public static TimeSpan Days(this int value)
{
    return new TimeSpan(value, 0, 0, 0);
}

public static TimeSpan Hours(this int value)
{
    return new TimeSpan(value, 0, 0);
}

public static TimeSpan Minutes(this int value)
{
    return new TimeSpan(0, value, 0);
}

//...

.

public static DateTime Ago(this TimeSpan value)
{
    return DateTime.Now.Add(value.Negate());
}

public static DateTime FromNow(this TimeSpan value)
{
   return DateTime.Now.Add(value);
}
Joel Coehoorn