In C#, The string class

ยท

3 min read

In C#, the string class provides a variety of methods to manipulate and handle strings. Here are some of the most commonly used methods:

Basic String Methods

Length Gets the number of characters in the string.

int length = myString.Length;

IndexOf Returns the index of the first occurrence of a specified character or string.

int index = myString.IndexOf('a');
int index = myString.IndexOf("example");

LastIndexOf Returns the index of the last occurrence of a specified character or string.

int index = myString.LastIndexOf('a');
int index = myString.LastIndexOf("example");

Substring Retrieves a substring from the string instance.

string subString = myString.Substring(startIndex);
string subString = myString.Substring(startIndex, length);

Contains Determines whether a string contains a specified substring.

bool contains = myString.Contains("example");

StartsWith Determines whether the beginning of the string matches a specified string.

bool startsWith = myString.StartsWith("example");

EndsWith Determines whether the end of the string matches a specified string.

bool endsWith = myString.EndsWith("example");

Replace Replaces all occurrences of a specified character or string with another character or string.

string replacedString = myString.Replace('a', 'b');
string replacedString = myString.Replace("old", "new");

ToUpper Converts the string to uppercase.

string upperString = myString.ToUpper();

ToLower Converts the string to lowercase.

string lowerString = myString.ToLower();

Trim Removes all leading and trailing white-space characters from the string.

string trimmedString = myString.Trim();

TrimStart Removes all leading white-space characters from the string.

string trimmedStartString = myString.TrimStart();

TrimEnd Removes all trailing white-space characters from the string.

string trimmedEndString = myString.TrimEnd();

Advanced String Methods

Split Splits the string into an array of strings based on a delimiter.

string[] splitStrings = myString.Split(' ');
string[] splitStrings = myString.Split(new char[] { ' ', ',' });

Join Concatenates the elements of a string array, using a specified separator between each element.

string joinedString = string.Join(", ", stringArray);

IsNullOrEmpty Indicates whether the specified string is null or an empty string.

bool isEmpty = string.IsNullOrEmpty(myString);

IsNullOrWhiteSpace Indicates whether a specified string is null, empty, or consists only of white-space characters.

bool isWhiteSpace = string.IsNullOrWhiteSpace(myString);

Format Replaces the format items in a string with the string representation of corresponding objects.

string formattedString = string.Format("Hello, {0}!", "World");

Compare Compares two specified string objects and returns an integer that indicates their relative position in the sort order.

int result = string.Compare(string1, string2);

CompareOrdinal Compares two specified string objects using the ordinal (binary) sort rules and returns an integer that indicates their relative position in the sort order.

int result = string.CompareOrdinal(string1, string2);

Equals Determines whether two string objects have the same value.

bool areEqual = string1.Equals(string2);

Copy Creates a new instance of a string with the same value as a specified string.

string copiedString = string.Copy(originalString);

Insert Inserts a specified string at a specified index position in this instance.

string newString = myString.Insert(5, "inserted");

Remove Deletes a specified number of characters from this instance.

string removedString = myString.Remove(startIndex);
string removedString = myString.Remove(startIndex, count);

String Interpolation (C# 6.0 and later) String interpolation provides a more readable and convenient syntax to create formatted strings.

string name = "World";
string interpolatedString = $"Hello, {name}!";

These methods and fe

ย