A few useful String extension methods
There are a couple of actions I find myself doing over and over again when it comes to strings and frankly, its becoming rather annoying writing the same lines of code over and over again for every application I create.
So, I have created a few of useful (at least for me) Extension Methods that encapsulate the reoccurring logic.
If you are not familiar with extension methods, its a great way introduced in .NET 3.5 to add functionality to an already existing type.
You typically use these methods to attach functionality to types you dont have access to their source code, those could be types in the .NET’s BCL, in a 3rd party library or even your own code you dont think this addition merits a change to the source.
So here are mine:
IsEmpty
This is a simple one born from pure laziness
. While i was working on an application recently I got tired writing
if (String.IsNullOrEmpty(str))…
for the hundredth time so I created an Extension method for the String type that does just that, now i can write it in shorter way:
If (str.IsEmpty())…
/// <summary>
/// Executes the String.IsNullOrEmpty on the current string
/// </summary>
/// <returns>True if either empty or null</returns>
public static bool IsEmpty(this String str)
{
return String.IsNullOrEmpty(str);
}
CsvToList
Working with configuration files quite allot I noticed that many times im passing a collection of (usually) comma separated values in a single string and then to parse this list im using the String.Split method.
What you usually need to do with the split method is provide a separator character or an array of these separators and optionally use the StringSplitOptions enumeration.
Doing this once is fine but what if you need to do this more than that? here’s my solution:
/// <summary>
/// Parses the String into a list of values separated by a comma (',')
/// </summary>
public static IEnumerable<string> CsvToList(this String str)
{
return CsvToList(str, ',');
}
/// <summary>
/// Parses the String into a list of values separated by the provided delimiter character
/// </summary>
/// <param name="delimiter">A character used in the String to separate between the values</param>
public static IEnumerable<string> CsvToList(this String str, char delimiter)
{
var delString = delimiter.ToString();
var delimiters = new[] { String.Format(" {0} ", delimiter), String.Format("{0} ", delimiter), delString };
return str.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
}
Take
Probably could have found a better name for it. This method lets you specify the start and end index of the sub-string to extract from the original string.
When manipulating strings and extracting sub-strings from other strings this method could come quite handy.
/// <summary>
/// Extension method allowing to state the start and end positions of the sub string to get from another string
/// </summary>
/// <param name="start">The start index of the sub-string</param>
/// <param name="end">The end index of the sub-string</param>
public static string Take(this String str, int start, int end)
{
return str.Substring(start, (end - start));
}
Conclusion
None of these methods I presented in this article are revolutionary by any means, their purpose is to make life a bit easier, especially when like with these examples you find yourself writing the same couple lines of code over and over again.
If at first it seems unnecessary, I promise that after making use of such extension methods youll find them both time saving and convenient to use.
So what extension methods are you using?
Source Code
You can download these methods here: code. I even included a method that creates a CSV String from an IEnumerable<string>
-
October 26, 2009 at 18:22 | #1A Few Useful Tridion Extension Methods « Life of a geek and a part-time poet
