Methods of String and Character in C#
String Methods
The `string` class in C# provides a wide range of methods for manipulation and querying.
Below are some commonly used ones.
Method Description Example
Contains(string value) Checks if the string contains `bool result =
a specified substring. "Hello".Contains("He");`
StartsWith(string value) Checks if the string starts `bool result =
with a specified substring. "Hello".StartsWith("He");`
EndsWith(string value) Checks if the string ends `bool result =
with a specified substring. "Hello".EndsWith("lo");`
Substring(int startIndex) Retrieves a substring `string sub =
starting from the specified "Hello".Substring(1); //
index. "ello"`
Substring(int startIndex, int Retrieves a substring of `string sub =
length) specified length. "Hello".Substring(1, 3); //
"ell"`
Replace(string oldValue, Replaces all occurrences of `string result =
string newValue) a substring. "Hello".Replace("l", "x"); //
"Hexxo"`
ToUpper() Converts the string to `string result =
uppercase. "Hello".ToUpper(); //
"HELLO"`
ToLower() Converts the string to `string result =
lowercase. "Hello".ToLower(); //
"hello"`
Trim() Removes all leading and `string result = " Hello
trailing whitespace ".Trim(); // "Hello"`
characters.
Split(char separator) Splits the string into an `string[] parts =
array of substrings. "A,B,C".Split(','); // ["A", "B",
"C"]`
Join(string separator, Joins an array into a single `string result =
IEnumerable<string>) string. [Link]("-", new[] { "A",
"B", "C" }); // "A-B-C"`
IndexOf(string value) Finds the index of the first `int index =
occurrence of a substring. "Hello".IndexOf("l"); // 2`
LastIndexOf(string value) Finds the index of the last `int index =
occurrence of a substring. "Hello".LastIndexOf("l"); //
3`
Length Gets the number of `int length =
characters in the string. "Hello".Length; // 5`
Equals(string value) Compares two strings for `bool isEqual =
equality. "Hello".Equals("hello",
[Link]
noreCase); // true`
IsNullOrEmpty(string value) Checks if a string is null or `bool isEmpty =
empty. [Link](""); /
/ true`
IsNullOrWhiteSpace(string Checks if a string is null, `bool isWhitespace =
value) empty, or whitespace. [Link](
" "); // true`
Concat(params string[]) Concatenates strings into `string result =
one. [Link]("Hello", " ",
"World"); // "Hello World"`
Format(string format, ...) Replaces format items with `string result =
specified values. [Link]("My name is
{0}", "John"); // "My name
is John"`
Character Methods
The `char` struct in C# provides methods to work with single characters.
Method Description Example
IsLetter(char c) Checks if the character is a `bool isLetter =
letter. [Link]('A'); // true`
IsDigit(char c) Checks if the character is a `bool isDigit =
digit. [Link]('1'); // true`
IsWhiteSpace(char c) Checks if the character is a `bool isSpace =
whitespace character. [Link](' '); //
true`
IsUpper(char c) Checks if the character is `bool isUpper =
uppercase. [Link]('A'); // true`
IsLower(char c) Checks if the character is `bool isLower =
lowercase. [Link]('a'); // true`
ToUpper(char c) Converts the character to `char upper =
uppercase. [Link]('a'); // 'A'`
ToLower(char c) Converts the character to `char lower =
lowercase. [Link]('A'); // 'a'`
IsControl(char c) Checks if the character is a `bool isControl =
control character. [Link]('\n'); // true`
IsPunctuation(char c) Checks if the character is a `bool isPunct =
punctuation mark. [Link]('.'); //
true`
IsSymbol(char c) Checks if the character is a `bool isSymbol =
symbol. [Link]('$'); // true`
GetNumericValue(char c) Converts a numeric `double numValue =
character to its numeric [Link]('5');
equivalent. // 5.0`
Parse(string s) Converts a string to a `char result =
character. [Link]("A"); // 'A'`