# String Manipulation

# Replacing a string within a string

Using the System.String.Replace (opens new window) method, you can replace part of a string with another string.

string s = "Hello World";
s = s.Replace("World", "Universe"); // s = "Hello Universe"

All the occurrences of the search string are replaced:

string s = "Hello World";
s = s.Replace("l", "L"); // s = "HeLLo WorLD"

String.Replace can also be used to remove part of a string, by specifying an empty string as the replacement value:

string s = "Hello World";
s = s.Replace("ell", String.Empty); // s = "Ho World"

# Changing the case of characters within a String

The System.String (opens new window) class supports a number of methods to convert between uppercase and lowercase characters in a string.

Note: The reason to use the invariant versions of these methods is to prevent producing unexpected culture-specific letters. This is explained here in detail (opens new window).


string s = "My String";
s = s.ToLowerInvariant(); // "my string"
s = s.ToUpperInvariant(); // "MY STRING"

Note that you can choose to specify a specific Culture (opens new window) when converting to lowercase and uppercase by using the String.ToLower(CultureInfo) (opens new window) and String.ToUpper(CultureInfo) (opens new window) methods accordingly.

# Finding a string within a string

Using the System.String.Contains (opens new window) you can find out if a particular string exists within a string. The method returns a boolean, true if the string exists else false.

string s = "Hello World";
bool stringExists = s.Contains("ello");  //stringExists =true as the string contains the substring 

Using the System.String.IndexOf (opens new window) method, you can locate the starting position of a substring within an existing string.
Note the returned position is zero-based, a value of -1 is returned if the substring is not found.

string s = "Hello World";
int location = s.IndexOf("ello"); // location = 1

To find the first location from the end of a string, use the System.String.LastIndexOf (opens new window) method:

string s = "Hello World";
int location = s.LastIndexOf("l"); // location = 9

# Removing (Trimming) white-space from a string

The System.String.Trim (opens new window) method can be used to remove all leading and trailing white-space characters from a string:

string s = "     String with spaces at both ends      ";
s = s.Trim(); // s = "String with spaces at both ends"

In addition:

  • To remove white-space only from the **beginning** of a string use: [`System.String.TrimStart`](https://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx)
  • To remove white-space only from the **end** of a string use: [`System.String.TrimEnd`](https://msdn.microsoft.com/en-us/library/system.string.trimend(v=vs.110).aspx)
  • Substring to extract part of a string.

    The System.String.Substring (opens new window) method can be used to extract a portion of the string.

    string s ="A portion of word that is retained";
    s=str.Substring(26);  //s="retained"
    s1 = s.Substring(0,5);  //s="A por"

    # Splitting a string using a delimiter

    Use the System.String.Split (opens new window) method to return a string array that contains substrings of the original string, split based on a specified delimiter:

    string sentence = "One Two Three Four";
    string[] stringArray = sentence.Split(' ');
    foreach (string word in stringArray)



    # Concatenate an array of strings into a single string

    The System.String.Join (opens new window) method allows to concatenate all elements in a string array, using a specified separator between each element:

    string[] words = {"One", "Two", "Three", "Four"};
    string singleString = String.Join(",", words); // singleString = "One,Two,Three,Four"

    # String Concatenation

    String Concatenation can be done by using the System.String.Concat (opens new window) method, or (much easier) using the + operator:

    string first = "Hello ";
    string second = "World";
    string concat = first + second; // concat = "Hello World"
    concat = String.Concat(first, second); // concat = "Hello World"