The .NET string class is quite comprehensive, yet some common string functions are missing or not entirely obvious. This article provides quick tips on using .NET strings.

Fill a String with Repeating Characters

To fill a string with repeating characters, use the string class constructor. For example, to fill a string with twenty asterisks:

string s = new string( '*', 20 );

Check for Blank String

A blank string can be represented by a null reference or empty string (String.Empty or “”). If you attempt to call a method on a null string, an exception will occur. Hence, to check for a blank string, you should use the new .NET v2.0 static function String.IsNullOrEmpty:

String.IsNullOrEmpty( s )

String.Empty vs. “”? It Doesn’t Matter

There has been endless debate on the Web whether it’s better to represent an empty string with String.Empty or blank quotes “”. However, tests show there is minimal performance difference between String.Empty and “” even when creating a billion empty strings.

Reverse a String

There has been extensive analysis on algorithms to reverse a string. The following is a good balance between speed and clarity and works well with Unicode and alternate character sets:

static public string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

Compare Strings

Because a string reference can be null, you should avoid using the equality symbol == or the Compare member function when comparing strings. Instead, use the static String.Compare method. This method has the advantage that it can handle null string references, compare strings ignoring case, and compare strings using a specific culture:

if (String.Compare( s1, s2, true ) == 0)

Convert String to Numeric Value

Each numeric data type such as int, Int32, double, etc. has a static TryParse method that converts a string to that data type without throwing an exception. The method returns a bool whether the string contained a value with the specified data type. For example:

string s = "42";
int i;
int.TryParse( s, out i );

Use Literal Strings for File Paths

A literal string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters. This makes literal strings ideal for file paths that naturally contain many backslashes. To create a literal string, add the at-sign @ before the string’s opening quote. For example:

string path = @"C:Program FilesMicrosoft Visual Studio 8SDKv2.0Bingacutil.exe";

String Right

Noticeably absent from the string class is the Right method. But you can replicate it easily using the Substring method. Here is a simple method that wraps this up nicely:

static string Right( string s, int count )
{
    string newString = String.Empty;
    if (s != null && count > 0)
    {
        int startIndex = s.Length - count;
        if (startIndex > 0)
            newString = s.Substring( startIndex, count );
        else
            newString = s;
    }
    return newString;
}

IndexOf Ignoring Case

The string’s IndexOf methods are all case-sensitive. Fortunately, the Globalization namespace contains the CompareInfo class that includes a case-insensitive IndexOf method. For example:

using System.Globalization;

string s1 = "C# is a GREAT programming language.";
string s2 = "great";

CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int i = Compare.IndexOf( s1, s2, CompareOptions.IgnoreCase );