Skip to main content

Strings

When programming, textual data is represented by strings. Textual data can be words such as "Apple", a short message such as "Hello World", or longer sentences such as "In high school, Jimmy learned some programming".

They are made up of a "string" of alphanumeric characters:

string

Defining Strings​

A series of characters surrounded by double quotes " ":

// Some cool words
string firstName = "Elizabeth";
string lastName = "Poggie";

// A cool sentance
string helloWorld = "Hello World!";

// strings can also contain numbers
string auntStatus = "Soon i will be 30 (AHHHHHH)";

String Concatenation​

Concatenation means joining strings together. The simplest way in C# is the + operator:

// string concatenation (wow)
string helloWorld;

helloWorld = "Hello" + "World"; // Output: "HelloWorld"
helloWorld = "Hello " + "World"; // Output: "Hello World" (spaces NEED to be inside the " ")
helloWorld = "Hello" + " " + "World"; // Output: "Hello World" (adding many things is cool)
helloWorld = "Hello" "World"; // ERROR - INVALID SYNTAX (Forgot the '+')
helloWorld = "Hello + " "World"; // ERROR - INVALID SYNTAX (+ do be in wrong spot)
helloWorld = "Hello " + "World " + ; // ERROR - INVALID SYNTAX (too many +)

Console.WriteLine(helloWorld);

This can be handy for building cute print statements:

// string concatenation (wow)
string preferredName = "Poggie";

Console.WriteLine("Hello " + preferredName); // English, Formal
Console.WriteLine("YO " + preferredName); // English, Casual
Console.WriteLine("Bonjour " + preferredName); // French, Formal
Console.WriteLine("Allô " + preferredName); // French, Casual

Programmers ❤️ efficiency: If preferredName ever changes, you only update it in one place instead of every line that uses it.

String Interpolation​

String interpolation is the most efficient way of combining strings with variables. The syntax is:

$"anything {variable} anything"

Breaking it down further:

  • variable is any C# variable that is currently defined. It can be of any type :^)
  • variable names are surrounded by
  • Notice that there is no space between the $ and the first quote "

Improving our earlier example:

// string interpolation (W O W)
string preferredName = "Poggie";

Console.WriteLine($"Hello {preferredName}"); // English, Formal
Console.WriteLine($"YO {preferredName}"); // English, Casual
Console.WriteLine($"Bonjour {preferredName}"); // French, Formal
Console.WriteLine($"Allô {preferredName}"); // French, Casual

Multiline Strings​

Sometimes, you want strings that take up many lines. Your have a 2 options:

// older version of C#
string cat = @"
/\_____/\
/ o o \
( == ^ == )
";

// newer versions of C# (C# 11 /.NET 7+)
string fish = """
.
\_____)\_____
/--v____ __`<
)/
'
""";

You can also combine multiline strings with string interpolation for fancier output messages:

string preferredName = "Poggie"; // From before

string email = $"""
----------------------------------------------
Hello {preferredName},

This is 100% not a template email (believe me)

Best,

Poggie
----------------------------------------------
""";

Resrouces​

Buschinelli, Mauricio. "420-1P6 - Week 2" John Abbott College, Fall 2023.

Bultena, Sandra and Youmna Badawy. "Strings." 420-SN1 Notes, John Abbott College, john-abbott-college.github.io/SN1-Notes/topics/04_variables/05_strings/05_strings.html