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

MethodDescriptionExample
ToUpper()Convert to uppercasename.ToUpper()
ToLower()Convert to lowercasename.ToLower()
Trim()Remove spacestext.Trim()
Replace()Replace texttext.Replace("a", "b")
Substring()Extract parttext.Substring(0, 3)
Contains()Check texttext.Contains("C#")
StartsWith()Check starttext.StartsWith("Hi")
EndsWith()Check endtext.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

You may also like...