0% found this document useful (0 votes)
7 views5 pages

Session 4 - Strings

The document provides an overview of C# strings, including their creation, concatenation methods, and properties. It explains string interpolation, accessing characters, and using special characters with escape sequences. Additionally, it highlights the distinction between adding numbers and concatenating strings using the + operator.

Uploaded by

adonai93cn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Session 4 - Strings

The document provides an overview of C# strings, including their creation, concatenation methods, and properties. It explains string interpolation, accessing characters, and using special characters with escape sequences. Additionally, it highlights the distinction between adding numbers and concatenating strings using the + operator.

Uploaded by

adonai93cn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Strings

C# Strings are used for storing text


The + operator can be used between strings to combine them. This is called concatenation.

string firstName = "Johan";


string lastName= “Vorster”;
Console.WriteLine(“Welcome ” + firstName + lastName);

You can also use the string.Concat() method to concatenate two strings:

string fullName = string.Concat(firstName, lastName);


Console.WriteLine(“Welcome ” + fullName);

// we could treat numbers as strings in some cases


string myX = “34”;
string myY = “45”;
Console.WriteLine(myX + myY); // the output will be 3445 – why?

Exercise: How could we change the code above so that a total value number is displayed?

- Option 1 hint – change the data types


- Option 2 hint – remember the lesson on type casting and conversion?

A string is an object
A string in C# is actually an object, which contain properties and methods that can perform certain
operations on strings. We will use only a few example methods today.

E.g. the length of a string can be found with the Length property:

string myTxt = "ABCDEFG";


Console.WriteLine("The length of the txt string is: " + myTxt.Length);
E.g. There are methods to convert the case of a string

string greet = "Hello World";


Console.WriteLine(greet.ToUpper()); // Outputs "HELLO WORLD"
Console.WriteLine(greet.ToLower()); // Outputs "hello world"

String Interpolation
NB. This is a newer feature introduced in Visual C# 6.0, so will not work in Visual Studio 2012, but
will work in later versions of Visual Studio
String interpolation allows programmers to substitutes values of variables into placeholders in a string.
Note that you do not have to worry about spaces, like with concatenation.
Also note that you have to use the dollar sign ($) when using the string interpolation method.

string firstName = "Johan";

string lastName = "Vorster";

string greeting = $"Welcome {firstName} {lastName}";

Console.WriteLine(greeting);

Access Characters Inside Strings


One way to think of a string is a sequence of individual characters. You can access the characters in a
string by referring to its index number inside square brackets [ ].

// To prints the first character from our existing string named greeting

Console.WriteLine(myString[0]); // Outputs "W"

NB: In C-type languages all indexes start with 0

- The first character in our example string is greeting[0]


- The second character is greeting[1]
- Etc
You can also find the index position of a specific character in a string, by using the IndexOf( ) method:

string myString = "Hello";

Console.WriteLine(myString.IndexOf("e")); // Outputs "1"

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:

// Assume that a name contains a first name, space, followed by last name

// We want to find and print out the last name

Console.Write(“Please enter your first and last name: “);

string fullName = Console.ReadLine( );

// Find location of the space

int charPos = fullName.IndexOf(" ");

// Extract the last name

string lastName = fullName.Substring(charPos+1);

Console.WriteLine(“Your last name = ” + lastName);


Special Characters
Strings must be written within quotes, If you try to do the following, C# will misunderstand this string,
and generate an error. To solve problems like this we use special characters.

// this causes an error because of the quotes

string txt = "We are the so-called "Vikings" from the north.";

We can use the backslash escape character \ to turn special characters like quotes into normal string
characters.

Escape character Result


\' ‘ Single quote
\" “ Double quote
\\ \ Backslash
\n New Line
\t Tab
\b Backspace

// Examples with quotes


string txt1 = "We are called \"Vikings\". ";
Console.WriteLine(txt1);

// Example with single quote


string txt2 = "It\'s John\' apple!.";
Console.WriteLine(txt2);

// Example with backslash


string txt3 = "The character \\ is called backslash.";
Console.WriteLine(txt3);

Exercise: In a previous lesson we made use of the \n special character. Try using the other special
characters and see how it affects your output.
Adding Numbers and Strings
We’ve seen this in previous lessons and also today at the start of the lesson. REMEMBER that C# uses
the + operator for both addition and concatenation.

If the data used are numbers, they are added. Strings are concatenated.

// 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 string concatenation:


string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)

You might also like