(C# Language) Strings and String Manipulation

In this tutorial we take common topics on strings, such as creating strings, on concatenation of strings, extraction of a substring, and truncation, null testing, searching a string within another, and comparison of two strings.
(Rev. 18-Jun-2024)

Categories | About |     |  


Table of Contents (top down ↓)

string vs String

string is an alias for System.String just like object is for System.Object and bool is for System.Boolean.

// other aliases in C# 

object    Alias for   System.Object
string    Alias for   System.String
bool      Alias for   System.Boolean
byte      Alias for   System.Byte
sbyte     Alias for   System.SByte
short     Alias for   System.Int16
ushort    Alias for   System.UInt16
int       Alias for   System.Int32
uint      Alias for   System.UInt32
long      Alias for   System.Int64
ulong     Alias for   System.UInt64
float     Alias for   System.Single
double    Alias for   System.Double
decimal   Alias for   System.Decimal
char      Alias for   System.Char

string can be used without requiring a directive for the System namespace.

You can use either of the two because they compile to the same System.String type. But experts recommend that string should be used in preference to System.String.

Video Explanation (see it happen!)

Please watch the following youtube video:

Strings are Immutable

A string cannot be altered once it has been created. If we create a string string s = "abcdefghi";, then s is a readonly string. Its characters cannot be changed or replaced. We cannot use s[0] = 'm' to change the first character. It is not possible, as well, to concatenate more characters to this string. This is a very subtle feature of the String class of .NET.

Assignment behaves differently with string types. Consider the code where a new string s1 is created by assignment of s. In this case s1 is NOT a reference to s. It is a DIFFERENT string that contains the same characters as the string s.

// creates a new string s1 
string s1 = s;

The compound assignment operator += also behaves differently here. Consider this code where we have written s += "hello".

// s contains a new string 
s += "hello";

In this case a new string is created by concatenation and stored back in s. The previously held string is orphaned and released for deletion by the garbage collector.

It is interesting to print the contents of s1 now. It can be verified that s1 still contains its own characters, although s has been altered. This confirms that assignment of strings creates new and independent strings.

// prints abcdefghi 


We have a problem if a lengthy loop keeps appending strings to create a longer string. It will cause too many strings to be created, and too many orphaned, making it tough for the garbage collector. It has an impact on the runtime efficiency of your program.

The problem can be solved by using the StringBuilder class. This class is intended for solving exactly the same problem. A StringBuilder keeps an extra capacity to allow concatenation without creating a new string. A StringBuilder can grow as per needs. The code shown here is a typical use of StringBuilder.

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 10; i++)

// prints 0123456789 

A for-loop keeps appending more strings to the StringBuilder.

The actual String object can be obtained from a StringBuilder by using its ToString method. As you can see we have used sb.ToString to print the string to console.

Commonly used functions of String

Let me now list some of the most commonly used functions of a string.

The Length function can be used to obtain the number of unicode characters held in the string instance.

String s2 = "hello";

// prints 5 
Console.WriteLine($"Length of s2 is {s2.Length}");

This code creates a string of five characters string s2 = "hello";. The function s2.Length prints 5.

We can use the static function String.IsNullOrWhiteSpace to determine if the string contains anything other than a null, or spaces or new lines. This is also very commonly used in day to day programming.

The substring function can be used to extract a substring from a given string. You can specify the start position of extraction. There is a second overload that allows you to specify the number of characters to be extracted.

Functions are also available for removing a portion of the string. A new string is created, but the old string is left un-touched.

There is a whole list of functions that are available for string manipulation. Replace for replacing occurrences of a string, Split for splitting a string to a string array, ToLower for creating a new string containing lowercase characters, ToUpper is for uppercase, Trim is for trimming whitespace or custom chars from an end or from both the ends of a string.

You will have to study the msdn documentation on need basis.

Replace - replace occurrences of a string
Split   - split a string to array
ToLower - convert string to a lowercase
ToUpper - covert string to uppercase
Trim    - trim end whitespace or custom chars

Searching in a String

The IndexOf function returns the index of the first occurrence of a string or char in a given string. It returns -1 if the search fails.

The Contains function returns true if there is at least one occurrence. It returns false if the search fails.

Comparison of Strings

Many times we need to compare if two strings are equal, i.e., contain exactly the same sequence of characters.

A static function called string.Equals can be used to make this comparison. One of its overloads allows us to make a comparison without regard to case. For this we have to pass an argument StringComparison.OrdinalIgnoreCase

This is a brief overview of strings. Thanks!

This Blog Post/Article "(C# Language) Strings and String Manipulation" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.