Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

String Methods in C#

Introduction

Strings are sequences of characters and are one of the most commonly used data types in any programming language. In C#, the String class provides various methods to perform operations on strings. This tutorial will cover some of the most commonly used string methods with detailed explanations and examples.

1. Length

The Length property returns the number of characters in a string.

Example:

string str = "Hello, World!";
int length = str.Length;

Output: 13

2. ToUpper and ToLower

The ToUpper method converts all characters in a string to uppercase, and the ToLower method converts all characters to lowercase.

Example:

string str = "Hello, World!";
string upperStr = str.ToUpper();
string lowerStr = str.ToLower();

Output:
upperStr = "HELLO, WORLD!"
lowerStr = "hello, world!"

3. Trim

The Trim method removes all leading and trailing white-space characters from the current string.

Example:

string str = " Hello, World! ";
string trimmedStr = str.Trim();

Output: "Hello, World!"

4. Substring

The Substring method retrieves a substring from the string instance. The substring starts at a specified character position and continues to the end of the string or for a specified length.

Example:

string str = "Hello, World!";
string subStr = str.Substring(7, 5);

Output: "World"

5. Replace

The Replace method replaces all occurrences of a specified string or character in the current string with another specified string or character.

Example:

string str = "Hello, World!";
string newStr = str.Replace("World", "C#");

Output: "Hello, C#!"

6. Split

The Split method splits a string into an array of substrings based on the characters in an array.

Example:

string str = "apple,orange,banana";
string[] fruits = str.Split(',');

Output: fruits = ["apple", "orange", "banana"]

7. Join

The Join method concatenates all the elements of a string array, using the specified separator between each element.

Example:

string[] fruits = { "apple", "orange", "banana" };
string str = string.Join(", ", fruits);

Output: "apple, orange, banana"

8. Contains

The Contains method returns a boolean indicating whether a specified substring occurs within the string.

Example:

string str = "Hello, World!";
bool containsWorld = str.Contains("World");

Output: true

9. StartsWith and EndsWith

The StartsWith method determines whether the beginning of the string instance matches a specified string, and the EndsWith method determines whether the end matches a specified string.

Example:

string str = "Hello, World!";
bool startsWithHello = str.StartsWith("Hello");
bool endsWithWorld = str.EndsWith("World!");

Output:
startsWithHello = true
endsWithWorld = true

10. IndexOf

The IndexOf method returns the zero-based index of the first occurrence of a specified substring within the string.

Example:

string str = "Hello, World!";
int index = str.IndexOf("World");

Output: 7