Sitemap
Ömer Faruk Didin

Ömer Faruk Didin

Hi, This is Ömer, I am a 3rd year student of Computer Engineering. I am interested in software and IT. I express my achievements.

C# Strings-2

3 min readSep 14, 2023

--

#13 In the last article we introduced “strings”, in this article we will look at “Access Strings” and “Special Characters” and finish the topic.

Press enter or click to view image in full size

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:

Press enter or click to view image in full size
Access Strings

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:

Press enter or click to view image in full size
Second Character(1)

You can also find the index position of a specific character in a string, by using the IndexOf() method:

Press enter or click to view image in full size
IndexOf()

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:

Press enter or click to view image in full size
Substring(), IndexOf()

Strings — Special Characters

Because strings must be written within quotes, C# will misunderstand this string, and generate an error:

Press enter or click to view image in full size
Special Characters

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:

Press enter or click to view image in full size
Escape Characters

The sequence \" inserts a double quote in a string:

Press enter or click to view image in full size
\”

The sequence \' inserts a single quote in a string:

Press enter or click to view image in full size
\’

The sequence \\ inserts a single backslash in a string:

Press enter or click to view image in full size
\\

Other useful escape characters in C# are:

Press enter or click to view image in full size
Escape Characters

I’ll end this article here, and in the next article we’ll talk about “Booleans” . Stay with the code.

--

--

Ömer Faruk Didin
Ömer Faruk Didin

Hi, This is Ömer, I am a 3rd year student of Computer Engineering. I am interested in software and IT. I express my achievements.