C# Strings – Getting Started with Strings

Working with strings is a very common task for most C# developers. The .NET Framework offers good variety of tools for working with strings, but care must be taken as there are several gotchas to trip up the beginner.

The first thing to note about strings in .NET is that they are Reference Types. Reference types are live on the managed heap in .NET and so they are managed by the .NET Garbage Collector and unlike Value types they are not automatically destroyed when they are out of scope.

Strings can be easily instantiated using the below syntax:

string s1;

Note that there is no default value for a string, thus you need to assign a value to the string before attempting to access it:

string s1;
Literal1.Text = s1; //Will give run-time error 

string s2 = ""; //This is as close as you can get to assigning nothing to a string;
Literal1.Text = s2;

There are numerous inbuilt methods in the .NET framework for dealing with strings. Namely:

Note that strings are immutable and thus the original string can never be changed. Therefore if you need to alter the string you will need to re-assign it a new string value:

string s1 = "all lower case";
s1.ToUpper() ; //s1 is unchanged
Literal1.Text = s1;  //outputs 'all lower case' since s1 is unchanged

string s2 = "all lower case";
s2 = s2.ToUpper() ; //s2 is now re-assigned the new uppercase string
Literal2.Text = s2; //outputs 'ALL LOWER CASE'

For more on string immutability see Strings are Immutable.

String Concatenation

C# offers a convenient method for concatenating a string using ‘+’ :

string s1 = "Hong ";
string s2 = "Kong";
Literal1.Text = s1 + s2;

Whilst this method is convenient it is not efficient for performing a large number of concatenations (since strings are immutable and are refenece types created on the managed heap each concatenation results in the creation of an extra string which is not automatically destroyed and stays in memory until the Garbage Collector destroys it).

To get around this limitation the .NET Framework provides the StringBuilder class which can efficiently concatenate strings using its Append() method:

 StringBuilder sb = new StringBuilder();
 sb.Append("Hong ");
 sb.Append("Kong");

Literal1.Text = sb.ToString();

This is by way of demonstration since there would not be any memory saving from concatentating two strings (in fact this can consume more memory since the StringBuilder consumes memory). In general concatenating more than four strings is normally when a StringBuilder should be used.

Escape Characters

In common with other C-based languages, C# strings may contain escape characters which qualify how the string should be output.
Escape characters begin with a backslash followed by a specific character.

Below are listed the various escape characters and their output:

\’ Inserts a single quote
\” Inserts a double quote
\\ Inserts a backslash
\a Triggers a system alert
\n Inserts a new line (on Windows systems)
\r Inserts a carriage return
\t Inserts a horizontal tab

Verbatim Strings

To preserve a string exactly as it is exactly as it was added simply add the @ character at the start of the string.

For example:

string outputStr =   "This site is named \'C# Help\'  "; 
Response.Write(outputStr);

This outputs

This site is named 'C# Help'

To output the string verbatim add the @ character at the start of the string.

string outputStr =   @"This site is named \'C# Help\'  "; 
Response.Write(outputStr);

This outputs

This site is named \'C# Help\'

Note that this will also preserve white space:

string outputStr =   @"This is a                  broken 
up 
             string";  
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!