CodeGym /Courses /C# SELF /Strings and Text: string type, working with strings

Strings and Text: string type, working with strings

C# SELF
Level 2, Lesson 2
Available

1. Digging Deeper into the string Type

The string type is one of the most used types in C#. Maybe even the most used. It's just an awesome type, because you can store text in these variables. And, unlike int and double, string objects have a ton of different functions, and with these functions you can do all sorts of cool stuff.

Also, every object in C# (literally every one) can be converted to a string. Or, to be more precise, every object in C# returns its text (string) representation. We'll come back to this type a bunch more times (it's just that useful and interesting), but today let's get to know the basics.

Creating string Variables

The string type is made for storing strings (text). To create a variable in your code that can hold text, you need to use this command:

string name;
Creating a variable of type string

Where name is the variable name.

Examples:

Command Description
string name; Creates a string variable name
string message; Creates a string variable message
string text; Creates a string variable text

Just like with int and double, you can use a short form to create several string variables at once:

string name1, name2, name3;
Creating string variables

2. Assigning Values to string Variables

To put a value into a string variable, use this command:

name = "value";
Assigning a value to a string variable

And here's the first difference from the types we've already learned. All string values are text strings and must be wrapped in double quotes.

Examples:

Command Note
string name = "Anya"; The name variable contains the text Anya
string city = "New York"; The city variable contains the text New York
string message = "Hello!"; The message variable contains the text Hello!

3. Initializing string Variables

Just like with int and double, you can initialize string variables right when you create them. By the way, you can do this with any type in C#. So we won't talk about this anymore.

string name1 = "string1", name2 = "string2";
Creating string variables

Example of creating and initializing variables

string name = "Anya", city = "New York", message = "Hello!";

You can only use variables that have a value

The C# compiler will complain if you declare a variable without giving it a value and then try to use it.

This code won't work:

string name;
Console.WriteLine(name); // Variable name is not initialized. The program won't compile.
int a;
a++; // Variable a is not initialized. The program won't compile.
double x;
double y = x; // Variable x is not initialized. The program won't compile.

4. Concatenation — Gluing Strings Together

There's a really cool and simple thing you can do with strings in C# — you can glue them together. This operation is called concatenation. Or, to make it easier to remember — Con-Cat-e-Nation. But people usually just call it "string concatenation" or even just "adding strings".

To glue two strings together, use the + sign. It's super easy:

"value1" + "value2"
Concatenating two strings

Examples:

Command Note
string name = "Anya" + "Anya"; name contains the string AnyaAnya
string city = "New York" + "Anya"; city contains the string New YorkAnya
string message = "Hello! " + "Anya"; message contains the string Hello! Anya

And of course, you can add a bunch of strings at once, and you can add strings and variables together too.

Examples:

string name = "Anya";                // name contains the string Anya
string city = "New York";           // city contains the string New York
string message = "Hello!" + city + name + city; // message contains the string Hello!New YorkAnyaNew York

5. Empty Strings

In the last example, you can see that the text in the message variable is kinda hard to read, since it's missing spaces. To add a space or a few spaces, just write them in your code and wrap them in double quotes. It's easier than it sounds:

" "
A string containing a single space

By the way, if you leave nothing between the quotes (just two double quotes in a row), you get what's called an "empty string":

""
Empty string

On one hand, it exists, but on the other, when you print it to the screen, nothing shows up. And when you glue it to other strings, nothing happens. It's kinda like the zero of strings.

6. Escaping Special Characters in Strings

You already know that strings are wrapped in double quotes. But what if you need to put quotes inside your string? If you just write quotes in the string, won't the compiler think that's the end of the string?

Exactly. It will. So quotes inside text are replaced with the pair \". And the code will look like this:

string quote = "He said: \"Hello!\"";
Console.WriteLine(quote); // He said: "Hello!"

Actually, it's a bit trickier. The \ character is considered a special (control) character inside strings. You can use it to add all sorts of "non-printable characters" like newlines, tabs, etc. And if you want to write the \ character itself, you need to write it twice.

Here are the 4 most common combos:

Syntax Meaning
\n Newline
\t Tab (indent)
\\ Literally \
\" Quotes inside a string

Example:

string multiline = "Line 1\nLine 2";
Console.WriteLine(multiline);

Will print:

// Line 1
// Line 2

7. String Interpolation ($-strings)

C# is a super modern language, and it lets you easily and nicely insert variable values into a string using interpolation — just add a $ before the quotes, and use curly braces {} inside the string. You already know about this.

But what you might not know is that you can use whole expressions and even call functions inside the curly braces! Example:

string name = "Olya";
int birthYear = 1999;

string info = $"My name is {name}, I'm {2025-birthYear} years old.";
Console.WriteLine(info); 	// My name is Olya, I'm 26 years old.

This approach makes your code way more readable and cuts down on mistakes (and parentheses). We'll talk more about interpolation in a separate lecture, but for simple cases, go ahead and use it.

8. String's Own Functions

The string type has its own functions (also called methods). There are a ton of them, and they make life easier for programmers. Today you'll meet some of the simplest ones. Example:

Function Description Example Result
str.Length String length "abc".Length → 3
str.ToUpper() To upper case "abc".ToUpper() → "ABC"
str.ToLower() To lower case "ABC".ToLower() → "abc"
str.Trim() Remove spaces at the edges " x y ".Trim() → "x y"

Examples of Working with String Functions

All these functions are called like this: variable.function(...) .

Get the length of a string:

string name = "Andrey";
int length = name.Length;
Console.WriteLine(length); // 6, since there are 6 letters

Convert a string to upper or lower case:

string original = "Hello";
Console.WriteLine(original.ToUpper()); // PRIVET
Console.WriteLine(original.ToLower()); // privet

Remove spaces at the edges (super useful when reading input):

string messy = "   hello   ";
Console.WriteLine(messy.Trim()); // "hello"

Each of these functions returns a new string, the old string doesn't change.

2
Task
C# SELF, level 2, lesson 2
Locked
Creating and Initializing String Variables
Creating and Initializing String Variables
2
Task
C# SELF, level 2, lesson 2
Locked
Working with Strings and Interpolation
Working with Strings and Interpolation
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION