C# Strings
C# Strings
In C#, a string is used to store text. Strings are objects of the String class and are immutable, meaning once created, they cannot be changed.
Declaring Strings
String Output
String Length
String Concatenation
✔ Using + Operator
✔ Using String.Concat()
✔ Using String Interpolation (Best)
Access Characters in String
String Methods
| Method | Description | Example |
|---|---|---|
ToUpper() |
Convert to uppercase | name.ToUpper() |
ToLower() |
Convert to lowercase | name.ToLower() |
Trim() |
Remove spaces | text.Trim() |
Replace() |
Replace text | text.Replace("a", "b") |
Substring() |
Extract part | text.Substring(0, 3) |
Contains() |
Check text | text.Contains("C#") |
StartsWith() |
Check start | text.StartsWith("Hi") |
EndsWith() |
Check end | text.EndsWith("!") |
String Comparison
Escape Characters
Verbatim Strings (@)
String and User Input
String Immutability
StringBuilder (Efficient for Changes)
Summary
✔ Strings store text
✔ Strings are immutable
✔ String interpolation is recommended
✔ StringBuilder is efficient for frequent changes
