I've never liked the implementation of Substring(), it's an exception happy, whiny baby of a method if ever there was one. But now with extension methods in .NET, I can make it better!
First, let's take a look at what I don't like. Substring() barfs on null values. Okay I know, it's up to the developer to check for nulls before calling this method, but I really hate checking for nulls every time I need to call a method, especially when I'm doing string manipulation, because then I'm then not able to chain methods together in a fluent style. I'd much rather it just returned an empty string. The next complaint that I have is that you have to specify a start index that is less then the length of the string itself. It makes sense I know, but now I also have to check the length before calling this method. The last complaint that I have is that when you specify the length of the substring, you also have to check that the number of characters that you've asked for doesn't exceed the actual number of characters left in the string. So there is yet another check you have to do before calling this method. So in short, you have to check for three different conditions before calling this method or your world will be filled with painful exceptions.
But have no fear, string.SafeSubstring() to the rescue!
1: public static class Extensions
2: {3: /// <summary>
4: /// This extension method works like string.Substring() except it doesn't throw
5: /// exceptions when you ask it to do something that it can't do, it simply returns
6: /// an empty string. If you set a startIndex that is greater than the string itself
7: /// it returns an empty string. If the string is null, it returns an empty
8: /// string. If you set a startIndex and length that's longer than what remains
9: /// of the string, it just returns the string of what is left.
10: /// </summary>
11: /// <param name="stringValue"></param>
12: /// <param name="startIndex">The zero based position of where you want the substring to start</param>
13: /// <param name="length">The length of string to be returned. If the remaining string is less than
14: /// what is requested, only the remaining string will be returned.</param>
15: /// <returns></returns>
16: public static string SafeSubstring(this string stringValue, int startIndex, int length)
17: {18: string newString = "";
19: 20: if (stringValue != null)
21: {22: int stringLength = stringValue.Length;
23: 24: if (startIndex < stringLength)
25: {26: if ((startIndex + length) < stringLength)
27: { 28: newString = stringValue.Substring(startIndex, length); 29: }30: else
31: {32: int remainingLength = stringLength - startIndex;
33: 34: newString = stringValue.Substring(startIndex, remainingLength); 35: } 36: } 37: } 38: 39: return newString;
40: } 41: } So now I can write a line of code like...
string shortLastName = lastName.SafeSubstring(2, 4).ToLower();without having to run a bunch of checks on 'lastName'. The following values of 'lastName' would've broken string.Substring():
- null (this would've caused a null reference error, duh)
- "Vu" (start index is greater than the length of the word)
- "McDee" (not enough characters exist in the substring)
- ""
- ""
- "Dee"
0 comments:
Post a Comment