string.md - brainchildservices/curriculum GitHub Wiki

SLIDE-TITLE

C# Strings

SLIDE-1

What is C# String?

In C#, a string is a series of characters that is used to represent text. It can be a character, a word or a long passage surrounded with the double quotes ". C# provides the String data type to store string literals.

SLIDE-2

What is String Length?

Returns the number of characters in a string. Example:(https://dotnetfiddle.net/yfdtQ6)

                        using System;

                        public class Program
                        {
                        	public static void Main()
                        	{
                        		string name = "Tiago";
                        int nameLength = name.Length;
                        Console.WriteLine("The name " + name + " contains " + nameLength + " letters.");
                        	}
                        }

                        Output: will show the stringname and stringlength.

SLIDE-3

What do you mean by String Concatenation?

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

SLIDE-4

What is String Interpolation?

Another option of string concatenation, is string interpolation, which substitutes values of variables into placeholders in a string. Note that you do not have to worry about spaces, like with concatenation:
image

SLIDE-5

How to access each character of a string?

You can access the characters in a string by referring to its index number inside square brackets [].
image

What are escape characters?

SLIDE-6

What is String.Format?

String.Format() method formats strings in a desired format by inserting objects and variables with specified space and alignments into another strings and literals. It is also often used to format strings into specific formats.

Example:(https://dotnetfiddle.net/2dBw4w)

                        using System;

                        public class Program
                        {
                        	public static void Main()
                        	{
                        		string name = "Kevin";
                        		int age = 20;
                        		double percentage = 90.25;

                        		Console.WriteLine();
                        		Console.WriteLine("----------------------------String Format-------------------------");
                        		Console.WriteLine();

                        		string myString=String.Format("My student {0}, who is aged {1} has scored {2} % of marks in his entrance exam",name,age,percentage);
                        		Console.WriteLine(myString);
                        		Console.WriteLine("My student {0}, who is aged {1} has scored {2} % of marks in his entrance exam",name,age,percentage);

                        	}
                        }

SLIDE-7

Assignment Questions:

Q1. Write a program to store the following sentence to a string
In honor of Nurses Week, show gratitude to those who take time to make sure we, and the people we love, are comfortable. They spend countless hours caring for us.
Print the length of the string.
Print the 5th character of the string.
Print the 6th character from last.
Print the index of the letter g
Print the sentence in UpperCase
Print the sentence in LowerCase

SLIDE-8

Q2. Write a program to store the name of a book(string), author of the book(string), price of the book(decimal), Number of copies sold(int), Year of release(int). Get these values input by the user.
Print the string: The book, NameOfBook was released in the year, ReleaseYear. The book was written by Author . Number_Of_Copies copies of the book was sold at Rs. Price per book.
Eg: The book, My Life was released in the year, 1998. The book was written by John Peters. 1990333 copies of the book was sold at Rs. 185 per book

Print the above string using:

  • String concatenation + operator
  • String interpolation $
  • String Format

SLIDE-9

Q3. Write a program to print the following paragraph using single Console.WriteLine() statement:
The dog, "Kevin" was sitting on a bench. As the wind picked up, John/James heard a rumbling sound from a distance.
"It was the minister//King", Peter exclaimed.
They saw the sun set in the ocean and it was getting "darker" and "darker".