0% found this document useful (0 votes)
1 views

String_and_Char_Methods

The document outlines various methods available for string and character manipulation in C#. It details string methods such as Contains, StartsWith, and Substring, along with character methods like IsLetter and IsDigit, providing descriptions and examples for each. This serves as a reference for developers working with string and character data types in C#.

Uploaded by

sarfarajkhan254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

String_and_Char_Methods

The document outlines various methods available for string and character manipulation in C#. It details string methods such as Contains, StartsWith, and Substring, along with character methods like IsLetter and IsDigit, providing descriptions and examples for each. This serves as a reference for developers working with string and character data types in C#.

Uploaded by

sarfarajkhan254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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. string.Join("-", 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",
StringComparison.OrdinalIg
noreCase); // true`
IsNullOrEmpty(string value) Checks if a string is null or `bool isEmpty =
empty. string.IsNullOrEmpty(""); /
/ true`
IsNullOrWhiteSpace(string Checks if a string is null, `bool isWhitespace =
value) empty, or whitespace. string.IsNullOrWhiteSpace(
" "); // true`
Concat(params string[]) Concatenates strings into `string result =
one. string.Concat("Hello", " ",
"World"); // "Hello World"`
Format(string format, ...) Replaces format items with `string result =
specified values. string.Format("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. char.IsLetter('A'); // true`
IsDigit(char c) Checks if the character is a `bool isDigit =
digit. char.IsDigit('1'); // true`
IsWhiteSpace(char c) Checks if the character is a `bool isSpace =
whitespace character. char.IsWhiteSpace(' '); //
true`
IsUpper(char c) Checks if the character is `bool isUpper =
uppercase. char.IsUpper('A'); // true`
IsLower(char c) Checks if the character is `bool isLower =
lowercase. char.IsLower('a'); // true`
ToUpper(char c) Converts the character to `char upper =
uppercase. char.ToUpper('a'); // 'A'`
ToLower(char c) Converts the character to `char lower =
lowercase. char.ToLower('A'); // 'a'`
IsControl(char c) Checks if the character is a `bool isControl =
control character. char.IsControl('\n'); // true`
IsPunctuation(char c) Checks if the character is a `bool isPunct =
punctuation mark. char.IsPunctuation('.'); //
true`
IsSymbol(char c) Checks if the character is a `bool isSymbol =
symbol. char.IsSymbol('$'); // true`
GetNumericValue(char c) Converts a numeric `double numValue =
character to its numeric char.GetNumericValue('5');
equivalent. // 5.0`
Parse(string s) Converts a string to a `char result =
character. char.Parse("A"); // 'A'`

You might also like