Learn C#_ Learn C#_ Strings Cheatsheet _ Codecademy
Learn C#_ Learn C#_ Strings Cheatsheet _ Codecademy
Cheatsheets / Learn C#
.ToUpper() in C#
In C#, .ToUpper() is a string method that converts string str2 = "This is C# Program
every character in a string to uppercase. If a character
xsdd_$#%";
does not have an uppercase equivalent, it remains
unchanged. For example, special symbols remain
unchanged. // string converted to Upper case
string upperstr2 = str2.ToUpper();
IndexOf() in C#
In C#, the IndexOf() method is a string method used string str = "Divyesh";
to find the index position of a specified character in a
string. The method returns -1 if the character isn’t
found. // Finding the index of character
// which is present in string and
// this will show the value 5
int index1 = str.IndexOf('s');
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-strings/cheatsheet 1/5
10/3/24, 9:45 PM Learn C#: Learn C#: Strings Cheatsheet | Codecademy
Bracket Notation
Strings contain characters. One way these char values // Get values from this string.
can be accessed is with bracket notation. We can even
string value = "Dot Net Perls";
store these chars in separate variables.
We access a specific character by using the square
brackets on the string, putting the index position of the //variable first contains letter D
desired character between the brackets. For example,
char first = value[0];
to get the first character, you can specify variable[0] .
To get the last character, you can subtract one from the
length of the string. //Second contains letter o
char second = value[1];
Substring() in C#
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-strings/cheatsheet 2/5
10/3/24, 9:45 PM Learn C#: Learn C#: Strings Cheatsheet | Codecademy
String Concatenation in C#
.ToLower() in C#
In C#, .ToLower() is a string method that converts string mixedCase = "This is a MIXED case
every character to lowercase. If a character does not
string.";
have a lowercase equivalent, it remains unchanged. For
example, special symbols remain unchanged.
// Call ToLower instance method, which
returns a new copy.
string lower = mixedCase.ToLower();
String Length in C#
The string class has a Length property, which returns string a = "One example";
the number of characters in the string.
Console.WriteLine("LENGTH: " + a.Length);
// This code outputs 11
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-strings/cheatsheet 3/5
10/3/24, 9:45 PM Learn C#: Learn C#: Strings Cheatsheet | Codecademy
String Interpolation in C#
Console.WriteLine(multipliedNumber);
// This code would output "The multiplied
ID is 1000."
String New-Line
Console.ReadLine()
The Console.ReadLine() method is used to get user Console.WriteLine("Enter your name: ");
input. The user input can be stored in a variable. This
method can also be used to prompt the user to press
string name = Console.ReadLine();
enter on the keyboard.
Comments
Comments are bits of text that are not executed. These // This is a single line comment
lines can be used to leave notes and increase the
readability of the program.
Single line comments are created with two /* This is a multi-line comment
forward slashes // . and continues until the end
Multi-line comments start with /* and end
of comment symbol is reached */
with */ . They are useful for commenting out
large blocks of code.
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-strings/cheatsheet 4/5
10/3/24, 9:45 PM Learn C#: Learn C#: Strings Cheatsheet | Codecademy
Console.WriteLine()
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-c-strings/cheatsheet 5/5