You are on page 1of 20

C# Fundamental Training : DAY 4.

1
Prepared By Nagma Khan
String

• It’s a collection of character and its used to store text value.


• Its of reference type.
• The maximum size of a String object in memory is 2-GB, or about 1
billion characters.
• String is immutable in nature. It means once its created, it cannot be
modified. Its read only.
• A string "modification" iStrings actually a new string creation.
• If multiple variables hold the same string value, CLR will allocate a
single memory location and save a reference so that the memory
usage can be minimized
Escape Sequence & Verbatim literal
• Escape Sequence are a combination of “\”
backslash and some character.
• They are used to print characters that have
special meaning.
• The @ special character serves as a verbatim
identifier. It has multiple implementation
1. To enable C# keywords to be used as
identifiers
2. To indicate that a string literal is to be
interpreted verbatim.
ESCAPE SEQUENCE & VERBATIM LITERAL

Special Characters
Because strings must be written within quotes, C# will misunderstand this string, and generate an error:
string txt = "We are the so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\) escape character turns special characters into string characters:
Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash
ESCAPE SEQUENCE & VERBATIM LITERAL

The sequence \" inserts a double quote in a string:


Example
string txt = "We are the so-called \"Vikings\" from the north.";
Console.WriteLine(txt);
The sequence \' inserts a single quote in a string:
Example
string txt = "It\'s alright.";
The sequence \\ inserts a single backslash in a string:
Example
string txt = "The character \\ is called backslash.";
ESCAPE SEQUENCE & VERBATIM LITERAL

Code Result Try it

\n New Line string txt = "Hello\nWorld!";


Console.WriteLine(txt);
\t Tab string txt = "Hello\tWorld!";
Other useful escape characters in C# are: Console.WriteLine(txt);
\b Backspace string txt = "Hel\blo World!";
Console.WriteLine(txt);
ESCAPE SEQUENCE & VERBATIM LITERAL

Verbatim Strings >> @


It is tedious to prefix \ to include every special characters. Verbatim string in C# allows a special
characters and line brakes. Verbatim string can be created by prefixing @ symbol before double quotes.
Example: Escape Sequence
string str = @"xyzdef\rabc";
string path = @"\\mypc\shared\project";
string email = @"test@test.com";
Escape Sequence & Verbatim literal

The @ symbol can also be used to declare a multi-line string.


Example: Multi-line String
string str1 = "this is a \n" + "multi line \n" + "string";
/ // Verbatim string
string str2 = @"this is a
multi line
string";
Escape Sequence & Verbatim literal

Please note that you cannot use a backslash to allow " in a verbatim string. If you wish to include @, then
use double double-quotes "" to include " in a verbatim string.

string text = "This is a \"string\" in C#."; // valid


string text = @"This is a "string." in C#."; // error
string text = @"This is a \"string\" in C#."; // error
string text = @"This is a ""string"" in C#."; // valid This is a “string” in C#.
Methods & Properties of string

• Replace() • IndexOf() • length


• Trim() • LastndexOf() • Join()
• TrimStart() • IndexOfAny() • Concat()
• TrimEnd() • CopyTo()
• ToLower() • Insert()
• ToUpper() • Remove()
• CompareTo() • Split()
• Contains() • Substring()
• StartsWith() • GetType()
• EndsWith() • Equals()
Methods & Properties of string

Replace(String, String)
The C# Replace() method is used to get a new string in which all occurrences of a specified Unicode
character in this string are replaced with another specified Unicode character. There are two methods of
Replace() method. You can replace string also.
Signature/Syntex
public string Replace(Char first, Char second)
public string Replace(String firstString, String secondString)
Parameter/Arguments
first: it is a first parameter of char type.
second: it is a second parameter of char type.
Return: It returns a string.
Example: string s1 = "Hello F#";
string s2 = s1.Replace('F','C');
string s1 = "Hello C#, Hello .Net, Hello Javatpoint";
string s2 = s1.Replace("Hello","Cheers");
Methods & Properties of string

Trim()
The C# Trim() method is used to remove all leading (from start)and trailing(End) white-space characters
from the current String object.
Signature
public string Trim()
public string Trim(params Char[])
Parameter
First method does not take any parameter. Second method takes a char array as parameter.
Return
It returns a string.
Example:
string s1 = " Hello C# ";
string s2 = s1.Trim(); \\ Hello C#
Methods & Properties of string

TrimStart()
The C# TrimStart() method is used to remove all leading(From start) occurrences of a set of characters
specified in an array from the current String object.
Signature
public string TrimStart(params Char[] ch)
Parameter
ch: it is a char array type parameter.
Return
It returns a string.
Example: string s1 = "Hello C#";
char[] ch = {'H'};
string s2 = s1.TrimStart(ch);
Console.WriteLine(s2);
Methods & Properties of string

TrimEnd(params Char[] ch)


The C# TrimEnd() method is used to remove all trailing(to End) occurrences of a set of characters specified
in an array from the current String object.
Signature
public string TrimEnd(params Char[] ch)
Parameter
ch: It takes a char array as parameter.
Return
It returns a string.
Example:
string s1 = "Hello C#";
char[] ch = {'#'};
string s2 = s1.TrimEnd(ch); \\Hello C
Console.WriteLine(s2);
Methods & Properties of string

ToLower()
The C# ToLowe() method is used to convert a string into lowercase. It returns a string in lower case.
Signature
public string ToLower()
public string ToLower(CultureInfo)
Parameter
First method does not take any parameter.
Return
It returns a string.
Example: string s1 = "Hello C#";
string s2 = s1.ToLower();
Console.WriteLine(s2); \\ hello c#
Methods & Properties of string

ToUpper()
The C# ToUpper() method is used to convert string into uppercase. It returns a string.
Signature
public string ToUpper()
public string ToUpper(CultureInfo)
Parameter
First method does not take any parameter.
Return
It returns a string
Example:string s1 = "Hello C#";
string s3 = s1.ToUpper();
Console.WriteLine(s3); \\ HELLO C#
Methods & Properties of string

CompareTo()
The C# CompareTo() method is used to compare String instance with a specified String object. It indicates
whether this String instance precedes, follows, or appears in the same position in the sort order as the
specified string or not.
Signature
public int CompareTo(String str)
Parameters
str: it is a string argument which is used to compare.
Return
It returns an integer value.
Example: string s1 = "hello";
string s2 = "hello"; string s3 = "csharp"; Console.WriteLine(s1.CompareTo(s2));
Console.WriteLine(s2.CompareTo(s3));
Methods & Properties of string

Contains()
The C# Contains() method is used to return a value indicating whether the specified substring occurs within
this string or not. If the specified substring is found in this string, it returns true otherwise false.
Signature
public bool Contains(String str)
Parameters
str: it is a string object which is used to check occurrence in the calling string.
Return
It returns boolean value either true or false.
Example:
string s1 = "Hello "; string s2 = "He";
string s3 = "Hi";
Console.WriteLine(s1.Contains(s2));
Console.WriteLine(s1.Contains(s3));
Programs To Practice

• Practice Escape sequence and verbatim string.


• Practice All Methods and Properties of string, discussed in this session.
• Write a C# program to accept a string from keyboard and print it

Best Of Luck
Thank You!!!

You might also like