Home

String Methods

Length: Returns the number of characters in the string

string text = "Hello";
int length = text.Length; // 5

Substring: Extracts a substring from a string.

string text = "Hello, World!";
string sub = text.Substring(7, 5); // "World"

IndexOf: Finds the index of the first occurrence of a specified substring.

string text = "Hello, World!";
int index = text.IndexOf("World"); // 7

Contains: Checks if the string contains a specified substring.

string text = "Hello, World!";
bool contains = text.Contains("Hello"); // true

Replace: Replaces all occurrences of a specified substring with another substring.

string text = "Hello, World!";
string replaced = text.Replace("World", "C#"); // "Hello, C#!"

ToUpper and ToLower: Converts the string to uppercase or lowercase.

string text = "Hello, World!";
string upper = text.ToUpper(); // "HELLO, WORLD!"
string lower = text.ToLower(); // "hello, world!"

Trim, TrimStart, TrimEnd: Removes whitespace from the beginning and/or end of the string.

string text = "   Hello, World!   ";
string trimmed = text.Trim(); // "Hello, World!"

Split: Splits the string into an array of substrings based on a specified delimiter.

string text = "apple,banana,cherry";
string[] fruits = text.Split(','); // ["apple", "banana", "cherry"]

Join: Concatenates the elements of a string array into a single string with a specified separator.

string[] fruits = { "apple", "banana", "cherry" };
string result = string.Join(", ", fruits); // "apple, banana, cherry"

StartsWith and EndsWith: Checks if the string starts or ends with a specified substring.

string text = "Hello, World!";
bool starts = text.StartsWith("Hello"); // true
bool ends = text.EndsWith("World!"); // true