I have created a useful set of string extensions in C# for doing many string operations like conversions to and from numbers, formatting, and escaping. Most of these were required by the application that I’m working on but are very useful for any purpose.
Converting a String to a Number
The main set of methods is for converting to numbers. I have methods for integers, longs, and doubles. They all use a very simple calling convention.
int num1 = "123".ToInt32(); int num2 = "123".ToInt32(0);
The first method simply calls the second with 0 as the default value. If the string cannot be converted to a number then the default value is returned. I would recommenced always specifying the default value yourself.
Here are the other numeric methods.
long l1 = "123".ToInt64(0); double d1 = "123.45".ToDouble(0); float f1 = "123.45".ToSingle(0)
There are also extended conversion methods that can be used. These have “Ex” on the end of the names. What makes them different is that you can provide a multiplier suffix on the end of the number.
int i1 = "123kb".ToInt32(0); long l2 = "100gb".ToInt64(0);
Valid suffixes are:
Suffix | Amount |
---|---|
k | Times 1000 |
kb | Times 1 Kilobyte |
m | Times 1 Million |
kb | Times 1 Megabyte |
b | Times 1 Billion |
gb | Times 1 Gigabyte |
t | Times 1 Trillion |
tb | Times 1 Terabyte |
Date and Time Values
There are a set of string to time conversion methods that attempt to convert a time value in a string to a DateTime. Depending on the function called, the
time format can be any of “yyyymmdd”, “yyyy/mm/dd”, “yyyy-mm-dd”, “hhmmss”, “hh:mm:ss”, or a combination of the date and time together by 0 or more spaces.
DateTime t1 = "2015/03/22 12:34:30".ToTime(DateTime.Now); DateTime t2 = "2015/03/22 12:34:30".ToDate(DateTime.Now); DateTime t3 = "2015/03/22 12:34:30".ToDatetime(DateTime.Now); DateTime t4 = "2015/03/22 12:34:30".ToTimeHourMinute(DateTime.Now); // t1 is 1/1/1 12:34:30 // t2 is 2015/03/22 00:00:00 // t3 is 2015/03/22 12:34:30 // t4 is 1/1/1 12:34:00
Checking Boolean Values
There are methods for checking if a string contains a boolean value. The default methods simply check for the words true or false using a case insensitive comparison. The extended methods, marked with Ex also use these values.
Boolean | Recognized values |
---|---|
false | false,f,no,n,0 |
true | true,t,yes,y,1 |
Because it takes more time to use the extended versions, they should only be used when really required.
string s1 = "true"; bool b1 = s1.IsTrue(); s1 = "yes"; b1 = s1.IsTrueEx();
Formatting
A quick formatting option has been provided by the Fmt extension method. It simply aliases the “string.Format” call.
string s1 = "Hello {0} {1}".Fmt("Trevor","Dennis");
Another useful method is “StandardFormat()” which takes an object as input and converts it into a string using a standard set of formats. Most of these are defaults but a few are specific. Dates are returned in a sortable and standard fashion as in “2015/03/22 12:00:00”. Floating point values are returned in number format. And strings are escaped.
C# Type | Formatting used |
---|---|
String | Returns the output of the extension method string.Escape(). |
Int32 | Returns the output of ToString() |
Int64 | Returns the output of ToString() |
UInt32 | Returns the output of ToString() |
UInt64 | Returns the output of ToString() |
Double | Returns the output of ToString(“r”) |
Single | Returns the output of ToString(“r’) |
Datetime | Returns the output of ToString(“yyyy/MM/dd HH:mm:ss”) |
Boolean | Returns the output of ToString() |
Guid | Returns the output of ToString() |
Other | All other data types return an empty string. |
Escaped Strings
String escaping is used to prevent control characters from messing up a string to prevent its parse-ability. I used this mainly when I have a string with tabs, line feeds, or carriage returns in it and must write that string to a text file like a CSV file. It converts them to identity-like values of &tab;, &lf;, and &cr;. That way, the line in the CSV doesn’t actually span multiple lines.
string escapedString = "this\nis\na\tbad string".Escape(); // Results in "this&cr;is&cr;a&tab;bad string". string original = escapedString.UnEscape();
Of course, these CSV files are meant for my code to read in again and not really meant for someone to load in Excel. They can certainly load them but they will see the identity markers.
Underscores
Another capability I required was a method of converting a string in Camel or Pascal case into something that used underscores between words. For example: thisIsAString converts to this_is_a_string. Spaces are also converted to underscores if they are found so “this is a string” is also converted to this_is_a_string. I used this method to convert WMI property names into something more readable by a user of my application.
The method also allows you to add a prefix and suffix to the returned string.
string value = "ThisIsAString".ToUnderscores("PREFIX_","_SUFFIX").ToUpper(); // value is "PREFIX_THIS_IS_A_STRING_SUFFIX".
Regular Expressions
There are a few regular expression based methods available to make matching and splitting strings a bit easier.
string[] result1 = "a,b,c,d".RegSplit(","); string[] result2 = "a,b,c,d".RegSplitIgnoreCase(","); Match m1 = "ab123def".RegMatch("^.*(\d+).*$"); Match m2 = "ab123def".RegMatchIgnoreCase("^.*(\d+).*$");
Source Code
A copy of the source code for this article can be found here. Be warned that some methods rely on other classes and may need to be removed to compile. I am working on a full release of the library for later.
Source code
I hope some of these prove useful to anyone who needs them!