C# String & string Manipulation
String Basics
In C#, a string is a sequence of characters stored as a single data type. Strings are immutable, meaning any operation that appears to modify a string actually creates a new string. string and String both can be used to represent string bath are same string preffered.
String Concatenation
Concatenation is the process of combining two or more strings into one. You can use the +
operator to concatenate strings.
|
|
String Interpolation
String interpolation is a more readable way to format strings, introduced in C# 6.0. It allows you to embed expressions inside string literals using the $
sign.
|
|
Verbatim String
A verbatim string literal is prefixed with @
and allows you to include escape sequences like \
as literal characters. It’s useful for file paths and multiline strings.
|
|
String Literal
A string literal is prefixed with """ and allows you to write string in mutilple line. It’s useful for multiline strings and complex string interpolation.
- starts with tripple quote then new line and the seperate """ ends
|
|
- When we want to interpolate some we can do so by prefixing by $ sign and then we can use {} for injection.
|
|
- There may be case when we have to both {} and interpolation the we can do so by prefixing by n number of $ sign and for interpolation we use same number of {} inside string.
|
|
Common String Methods
Replace
Replaces all occurrences of a specified string or character in the original string with another string or character.
|
|
Insert
Inserts a specified string at a specified index position in the original string.
|
|
Remove
Removes a specified number of characters from a specified index position in the original string.
|
|
Substring
Retrieves a substring from the original string. The substring starts at a specified index position and has a specified length.
|
|
ToUpper
Converts the string to uppercase.
|
|
Length
Gets the number of characters in the string.
|
|
Additional Resources
For more detailed information on strings and their manipulation in C#, visit the Official Documentation.