Working with Strings in C#: A Comprehensive Guide to String Manipulation

Master string manipulation in C#. This tutorial covers string creation, immutability, and explores a wide range of built-in methods for string operations (concatenation, comparison, substring extraction, etc.), providing a comprehensive guide to efficient string handling in C#.



Working with Strings in C#

Understanding C# Strings

In C#, a string is an object of the `System.String` class. It represents a sequence of characters. Strings are immutable; once created, their value cannot be changed (any operation that appears to modify a string actually creates a new string object). C# provides a rich set of built-in methods for manipulating strings, covering tasks such as concatenation, comparison, substring extraction, searching, trimming, and more. The `string` keyword is an alias for `System.String`, so both can be used interchangeably.

Creating Strings in C#

You can create strings using string literals or the `String` constructor. String literals are enclosed in double quotes (" ").

Example String Creation

string str1 = "Hello"; //String literal
string str2 = new string(new char[] { 'W', 'o', 'r', 'l', 'd' }); //String constructor

Common C# String Methods

C# provides a wide array of built-in methods for string manipulation. Here's a summary; each method would link to more detailed information in a full tutorial.

Method Description
Clone() Creates a shallow copy of the string.
Compare() Compares two strings.
CompareOrdinal() Compares two strings based on their numeric values.
CompareTo() Compares this string to another string.
Concat() Concatenates two or more strings.
Contains() Checks if a string contains a specific substring.
Copy() Creates a new string with the same value.
CopyTo() Copies a portion of the string to a character array.
EndsWith() Checks if the string ends with a specific substring.
Equals() Compares two strings for equality.
Format() Creates a formatted string.
GetEnumerator() Returns an enumerator to iterate through the characters.
GetHashCode() Returns a hash code for the string.
GetType() Gets the type of the object (System.String).
GetTypeCode() Gets the TypeCode for the string.
IndexOf() Finds the index of the first occurrence of a specified substring.
Insert() Inserts a substring at a specified index.
Intern() Retrieves a reference to a string from the string pool.
IsInterned() Checks if a string is interned.
IsNormalized() Checks if the string is in Unicode normalization form C.
IsNullOrEmpty() Checks if a string is null or empty.
IsNullOrWhiteSpace() Checks if a string is null, empty, or whitespace.
Join() Concatenates an array of strings with a specified separator.
LastIndexOf() Finds the last occurrence of a specified character.
LastIndexOfAny() Finds the last occurrence of any character from an array.
Normalize() Returns a normalized version of the string (Unicode normalization).
PadLeft() Pads a string on the left with spaces or a specified character.
PadRight() Pads a string on the right with spaces or a specified character.
Remove() Removes characters from a string.
Replace() Replaces occurrences of a substring with another substring.
Split() Splits the string into an array of substrings.
StartsWith() Checks if the string starts with a specific substring.
Substring() Extracts a substring.
ToCharArray() Copies the string to a character array.
ToLower() Converts the string to lowercase.
ToLowerInvariant() Converts the string to lowercase using invariant culture rules.
ToString() Returns a string representation of the string (the string itself).
ToUpper() Converts the string to uppercase.
ToUpperInvariant() Converts the string to uppercase using invariant culture rules.
Trim() Removes leading and trailing whitespace.
TrimEnd() Removes trailing characters.
TrimStart() Removes leading characters.

Conclusion

The `System.String` class in C# offers a vast array of methods for manipulating strings. Understanding these methods is essential for efficient string handling in C# applications.