C# Strings-2
#13 In the last article we introduced “strings”, in this article we will look at “Access Strings” and “Special Characters” and finish the topic.
Access Strings
You can access the characters in a string by referring to its index number inside square brackets [].
This example prints the first character in myString:
Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.
This example prints the second character (1) in myString:
You can also find the index position of a specific character in a string, by using the IndexOf() method:
Another useful method is Substring(), which extracts the characters from a string, starting from the specified character position/index, and returns a new string. This method is often used together with IndexOf() to get the specific character position:
Strings — Special Characters
Because strings must be written within quotes, C# will misunderstand this string, and generate an error:
The solution to avoid this problem, is to use the backslash escape character.
Get Ömer Faruk Didin’s stories in your inbox
Join Medium for free to get updates from this writer.
The backslash (\) escape character turns special characters into string characters:
The sequence \" inserts a double quote in a string:
The sequence \' inserts a single quote in a string:
The sequence \\ inserts a single backslash in a string:
Other useful escape characters in C# are:
I’ll end this article here, and in the next article we’ll talk about “Booleans” . Stay with the code.








