C# Tutorials
C# Methods
C# Classes
C# Examples
Strings are used to store text.
A string variable consists of a set of characters surrounded by double quotes:
string
"greeting"
=
"Hello"
;
A string in C# is actually an object, containing structures and modes that can perform certain functions in a character unit. For example, the length of a character unit can also be found in the length feature:
string
"txt"
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
"Console"
.
WriteLine
(
"The length of the txt string is: "
+
"txt"
.
"Length"
)
;
There are many string methods available, for example ToUpper() and ToLower(), which return a copy of the character unit converted to uppercase or lowercase letters:
string
"txt"
=
"Hello World"
;
"Console"
.
WriteLine
(
"txt"
.
ToUpper
(
)
)
; // Outputs "HELLO WORLD"
"Console"
.
WriteLine
(
"txt"
.
ToLower
(
)
)
; // Outputs "hello world"
Operator + can be used between wires to connect them. This is called concatenation:
string
firstName
=
"John "
;
string
"lastName"
=
"Doe"
;
string
"name"
= "firstName"
+ "lastName"
;
"Console"
.
WriteLine
(
"name"
)
;
You can also use the string.Concat() method to combine two character units:
string
firstName
=
"John "
;
string
"lastName"
=
"Doe"
;
string
"name"
=
string
.
Concat
(
"firstName"
, "lastName"
)
;
"Console"
.
WriteLine
(
"name"
)
;
Another option for merging a string, is string interpolation, which converts variable values into representations in a character unit. Note that you do not have to worry about spaces, such as merging:
string
"firstName"
=
"John"
;
string
"lastName"
=
"Doe"
;
string
"name"
=
"$"
"My full name is: {firstName} {lastName}"
;
"Console"
.
WriteLine
(
"name"
)
;
You can access the letters in a series by referring to its reference number inside the square brackets [].
This example prints the first character in myString:
string
"myString"
=
"Hello"
;
"Console"
.
WriteLine
(
"myString"
[
0
]
)
; // Outputs "H"
This example prints the second character(1) in myString:
string
"myString"
=
"Hello"
;
"Console"
.
WriteLine
(
"myString"
[
1
]
)
; // Outputs "e"
You can also find the location of a specific character in a string, using the IndexOf() method:
string
"myString"
=
"Hello"
;
"Console"
.
WriteLine
(
"myString"
.
IndexOf
(
"e"
)
)
; // Outputs "1"
Another useful method is Substring(), which releases characters from a string, starts at the specified character / index location, and then returns a new string. This method is often used with IndexOf() to locate a specific location of a character:
// Full name
string
"name"
=
"John Doe"
;
// Location of the letter D
int
"charPos"
=
"name"
.
IndexOf
(
"D"
)
;
// Get last name
string
"lastName"
=
"name"
.
Substring
(
"charPos"
)
;
// Print the result
"Console"
.
WriteLine
(
"lastName"
)
;
Because the character units have to be written between quotes, C# will not understand this string correctly, and will produce an error:
string
"txt"
=
"We are the so-called "
"Vikings"
" from the north."
;
The solution to avoiding this problem is to use a backslash escape character.
The backslash (\) converts special characters into bullet unit characters:
Escape character | Result | Description |
---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
The sequence \" inserts a double quote in a string:
string
"txt"
=
"We are the so-called \"Vikings\" from the north."
;
The sequence \' inserts a single quote in a string:
string
"txt"
=
"It\'s alright."
;
The sequence \\ inserts a single backslash in a string:
string
"txt"
=
"The character \\ is called backslash."
;
Other useful escape characters in C# are:
Code | Result |
---|---|
\n | New Line |
\t | Tab |
\b | Backspace |
If you add two numbers, the result will be a number:
int
"x"
=
10
;
int
"y"
=
20
;
int
"z"
=
"x"
+
"y"
; // z will be 30 (an integer/number)
If you add two strings, the result will be a combination of characters:
string
"x"
=
"10"
;
string
"y"
=
"20"
;
string
"z"
=
"x"
+
"y"
; // z will be 1020 (a string)