C# Language

String Manipulation

Changing the case of characters within a String

The System.String 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.

Example:

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

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

Finding a string within a string

Using the System.String.Contains 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 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 method:

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

Removing (Trimming) white-space from a string

The System.String.Trim 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:

Substring to extract part of a string.

The System.String.Substring 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"

Replacing a string within a string

Using the System.String.Replace 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"

Splitting a string using a delimiter

Use the System.String.Split 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)
{
    Console.WriteLine(word);    
}

Output:

One
Two
Three
Four

Concatenate an array of strings into a single string

The System.String.Join 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 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"

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow