Strings in C# are sequences of characters, widely used for text manipulation. They are immutable, meaning their values cannot be changed after creation.
String Length:
You can find the length of a string in C# using the Length property of the string, which returns the number of characters in the string.
Example:
string myString = "Hello, C#";
int length = myString.Length; // length will be 9
C# String Concatenation:
String concatenation in C# is the process of combining two or more strings into a single string using the + operator or the string.Concat() method.
You can concatenate strings with numbers in C# using the + operator, but you should convert numbers to strings explicitly using ToString() method or string interpolation.
Example:
int age = 30;
string message = "I am " + age.ToString() + " years old."; // message will be "I am 30 years old."
C# String Interpolation:
String interpolation is a convenient way to embed expressions and variables directly within a string using the $ symbol and curly braces {}.
Example:
int apples = 5;
string output = $"I have {apples} apples."; // output will be "I have 5 apples."
C# Access Strings:
You can access individual characters of a string by their index using square brackets [].
Example:
string myString = "C# Programming";
char firstChar = myString[0]; // firstChar will be 'C'
Strings - Special Characters:
C# allows you to include special characters in strings using escape sequences, like \n for a newline or \" for a double quote within a string.
Example:
string message = "This is a special\ncharacter: \"";