10 Most Commonly Used String Methods in C#

Here in this post, you will get to know some most used C# string methods that everyone should be aware of while working on strings. These functions let you do manipulation on text in very efficient ways. Using these functions, you can make your code easy to organize.

C# String Functions With Examples

1. Substring(int startIndex):

It allows you to extract a substring from the string starting at the specified index.

string txt = "Hello, World";
string result = txt.Substring(7);
Console.WriteLine(result);
//Output: "World"

2. Replace(string oldValue, string newValue):

This function replaces all occurrences of a specified string with the specified string.

string txt = "Hello, World";
string result = txt.Replace("World", "Code");
Console.WriteLine(result);
//Output: "Hello, Code"

3. Trim():

Trim() function removes all leading and trailing white-space characters from the string.

string txt = "   Hello, World   ";
string result = txt.Trim();

Console.WriteLine(result);
//Output: "Hello, World"

4. ToUpper() and ToLower():

ToUpper() converts a string to uppercase and ToLower() converts a string to lowercase.

string txt = "Hello, World";
string upperResult = txt.ToUpper(); // "HELLO, WORLD!"
string lowerResult = txt.ToLower(); // "hello, world!"
Console.WriteLine(upperResult);
Console.WriteLine(lowerResult);
//Output: "HELLO, WORLD"
//Output: "hello, world"

5. Contains(string value):

Contains() function checks if the string contains a specified substring. It returns true if a substring is contained by a string else returns false.

string txt = "Hello, How are you?";
bool result = txt.Contains("How");
Console.WriteLine(result);
//Output: true

6. StartsWith(string value) and EndsWith(string value):

StartsWith() function checks whether the string starts with a specified substring. Similarly EndsWith() checks whether the string is ending with a specified substring.

string txt = "Hello, World";
bool resultStarts = txt.StartsWith("Hello");
bool resultEnds = txt.EndsWith("World!");
Console.WriteLine(resultStarts);
Console.WriteLine(resultEnds);
//Output: true
//Output: true

7. Join(string separator, string[] value):

Join() concatenates the elements of a string array, using a specified separator between each element.

string[] wordsArray = { "Tom", "Dick", "Harry" };
string result = string.Join(", ", wordsArray); 
Console.WriteLine(result);
//Output: "Tom, Dick, Harry"

8. Concat():

Concat() function is used to concatenate two or more strings into a single string.

string str1 = "Hello, ";
string str2 = "World";
string result = string.Concat(str1, str2);
Console.WriteLine(result);
//Output: "Hello, World"

9. Split(char separator):

Split() function splits a string into an array of substrings based on a delimiter.

string txt = "Tom,Dick,Harry";
string[] result = txt.Split(','); 
//Output: ["Tom", "Dick", "Harry"]

10. IndexOf(string value):

Using IndexOf () function, you can get index of the first occurrence of a specified substring in a string. It uses zero based index system.

string txt = "Hello, World!";
int result = txt.IndexOf("World");
Console.WriteLine(result);
//Output: 7

Being hands on, on the above string functions can help you enhance application performance and data validation. Apart from above mentioned functions, you can use C# Left, Right and Mid functions also to bring ease in your program.

Leave a Reply