Professional Documents
Culture Documents
1
Prepared By Nagma Khan
String
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
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.
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
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
Best Of Luck
Thank You!!!