Raj Aththanayake's Blog Raj Aththanayake's Blog | Working with Strings in C#.NET
Home > C#

Working with Strings in C#.NET

30. May 2010

Strings, strings and strings. They are all over in your application. You cannot avoid them at all. This article is about some tips on how you should use strings within your application. Most developers know these tips, but I still see some developers write inefficient code.

 

 Before we drill into much deeper, would you recommend using String over string? For example…

String apple;
string apple;

Not really, string is simply an alias (shorthand) for the Common Type System (CTS) System.String class, which represents a sequence of characters. You can use them interchangeably in your code. For example…

String x = string.Copy("x");
string y = String.Copy("y");

 

String concaternations

Often developer concatenate string using + operator. Some developer use StringBuilder class in System.Text namespace.

string concat = "Apple" + ", " + "Orange";
StringBuilder sb = new StringBuilder();
sb.Append("Apple");
sb.Append(", ");
sb.Append("Mango");

So which one you should be using? It depends. For example if you have few strings that need to concatenate, then ‘+’ operator would just be fine. However if you have so many of them, it is better to use the StringBuilder class. The reason is that direct string concatenation will always consume more memory as it creates String objects for every instance that it creates. On the other hand StringBuilder is a complex type, which require instantiating once (also take up a negligible start-up time), and Append method is much faster, better CPU and efficient memory usage. Because of the tiny start up cost of instantiating StringBuilder class, it is preferable to use ‘+’ operators for when concatenating only few strings.

Below is an excellent article on string concatenations performances

The conclusion to be drawn from these test results is really very straightforward. You should be using the StringBuilder class for all but the most trivial string concatenation (or replace) operations. The extra effort required to use the StringBuilder class is negligible and is far outweighed by the potential performance and scalability benefits to be gained.

More…. http://msdn.microsoft.com/en-us/library/aa302329.aspx

Also avoid String.Format() for string concatenation. They are very useful for globalization and other tasks, however not meant to be used for appending strings to each other.

 

Empty Strings

string a = "";
string c = String.Empty;

There is no much performance differences on any of these. If you want the maximum efficiency and better use of CPU memory use String.Empty. String.Empty is a static and read only field. It does not create any object. Because when you use an empty string “”, it still creates a new object. If you are performing a string comparison with an empty string, for example...

if (testString == String.Empty)
{
     testString = "Test string is empty";
}

if (StringA.Equals(String.Empty))
{ 
     testString = "Test string is empty"; 
}

if (testString.Length == 0)
{
      testString = " Test string is empty";
}

Some of you would argue the lack of readabilty on Legth == 0, but the truth is the Length comparison is the most efficient. If you need to check for null or empty string then String.IsNullOrEmpty() is also better on performances.

 

Securing your strings

Common string classes in .NET framework store string in memory. If you are dealing with very sensitive data, then storing in memory is not the most secure way. For this you need to use SecureString class which is System.Secure name space.

System.Security.SecureString secureString = new System.Security.SecureString();
secureString.AppendChar('S');
secureString.AppendChar('E');
secureString.AppendChar('C');
secureString.AppendChar('U');
secureString.AppendChar('R');
secureString.AppendChar('E');

Unfortunately you cannot assign a string to the secure class. Please find more information on Secure String below.

http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx

 

Compare Strings

Many developers tend to use the case insensitivity to compare two strings. This is not really efficient when comparing two strings.

if (testStringA.ToUpper() == testStringB.ToUpper())
{
     // Your code goes here
}

The regular expression engine in .NET is extremely good. but is not the most efficient method of simply comparing two strings. The below code is bit of a overkill.

var pattern = "Regex pattern.";
var string_value = "Hi! This is a test.";
var b = Regex.IsMatch(string_value, pattern);
Console.WriteLine("{0}", b);

However there is a much efficient way to compare strings using String.Compare method.

if (String.Compare(testStringA, testStringB, true) == 0)
{
      // Your code goes here
}

The below comparison does the same thing, but it is the most efficient.

if (String.Equals(StringA, StringB, StringComparison.OrdinalIgnoreCase))
{
	// Your code goes here                
}

More information is on ordinal string comparison is below.

http://blogs.msdn.com/b/bclteam/archive/2007/05/31/string-compare-string-equals-josh-free.aspx

 

String Literals and String Constants

I mentioned previously that string literals create a new string object. It doesn’t mean that you should not be using string literals at all. For example using string literals directly in your code assist readability of your code and have better performance than using string constants. Having said that if you have number of strings being used in you code over and over again, it is still better to define the string constants as the performance gain I mentioned above is not really significant. As you know changing strings in many places is often painful than declare it once, and use it wherever and wherever you want is lot easier.

private const string testString = "sampleString";
StringA = testString;

StringA = “sampleString” //efficient

  

Summary

 

The above are some of the examples that I tried by my self to prove better and efficient way of string manipulations. However there are many other string manipulations techniques which I haven’t mentioned here and worth trying. For example splitting strings, replacing strings, removing strings etc. It is very easy to check these operations, and all you need is micro-benchmarking fascilty. I use System.Diagnostics.Stopwatch class for benchmarking but there are other techniqes as well.

As a final note always be mindful when perform codding, and use these techinques so you would write efficient C# code.

C#

Comments

Nick
Nick
5/30/2010 7:37:59 PM #
Hey Raj
Great information. Very informative. Thanks a lot. Any chance you would add some more string manipulation performance considerations?
5/31/2010 1:54:23 AM #
Great stuff didn't know there was a secure string class.

I always just encrypted data myself. Will try find an effective use for it now Smile
Noel
Noel
6/1/2010 3:58:34 PM #
NIce article. I like to use StringBuilder almost every concatenation operation but never knew it has a start up cost which is significant when few strings get concatenated. Thanks for the info.
Comments are closed