You are on page 1of 30

Visual Programming

Lecture 8

Jawad Rafeeq
Jawadrafeeq@ciitvehari.edu.pk
Outline
C# String
• A string is represented by class System.String. 
•  The maximum size of String object in memory is 2GB or about 1
billion characters. 

1. string s1 = “Visual Programming”;


2. System.String s2 = “By Using C Sharp”;
char[] chars = { 'A', ‘E', ‘R', ‘O', ‘P', ‘L' };  
3. string name = new string(chars); /// AEROPL
Reading String from User-Input:
• static void Main(string[] args)
• {

• Console.WriteLine("Enter the String");

• String usersinput = Console.ReadLine();
• Console.WriteLine(usersinput);

• }
To save directory’s path
• // using double slash \\
• string str2 = “C:\\Users\\Winrar\\winrar.exe";
• Console.WriteLine(str2);
Create a string using concatenation:
• string s1 = “Economy";
• string s2 = “is";
• string s3 = “very";

• // concatenation
• string str = s1 + s2 + s3 + “good";
Create a string using ToString Method
 
• int age = 33;  
• string authorInfo = age.ToString();  
• Console.WriteLine(authorInfo);  
Show all characters of a string using C# (not \
0)
string nameString = “Visual Programming is very interesting";  

for (int counter = 0; counter < nameString.Length; counter++)  
Console.WriteLine(nameString[counter]); 

foreach(char i in nameString)
{
System.Console.Write(i);
}
How to make an Empty String
• string empStr = string.Empty;  
• string empStr2 = ""; 
C# Strings built-in methods
Using String.Equals
• If both strings are equal, the method returns true; else returns false.

• string aa = "ascd";
• string bb = "ascd";
• bool r = string.Equals(aa, bb);
• if(r==true)
• System.Console.WriteLine("Both are Equal");
Using String.Equals (Ignorecase)
• If both strings are equal, the method returns true; else returns false.

• string aa = "ascd";
• string bb = “ASCD";
• bool r = string.Equals(aa, bb, StringComparison.OrdinalIgnoreCase);
• if(r==true)
• System.Console.WriteLine("Both are Equal");
Using String.Compare (Extra functionality)
• string str1 = "csharp";
• string str2 = "csharp";

• // Use String.Compare method


• if (String.Compare(str1, str2) == 0)
• Console.WriteLine("Both strings have same value.");
• else if (String.Compare(str1, str2) < 0)
• Console.WriteLine("str2 is greater than str1");
• else if (String.Compare(str1, str2) > 0)
• Console.WriteLine("str1 is greater than str2"); ;
Using String.Compare (ignorecase)
• string str1 = "csharp";
• string str2 = "CSHARP";

• // Use String.Compare method


• if (String.Compare(str1, str2, true) == 0)
• Console.WriteLine("Both strings have same value.");
• else if (String.Compare(str1, str2) < 0)
• Console.WriteLine("str2 is greater than str1");
• else if (String.Compare(str1, str2) > 0)
• Console.WriteLine("str1 is greater than str2");
Using String.copy, String.clone, and default
method
• str1 = str2; public object Clone( --- )
{
• str2 = String.Copy(str1); Both are same
return this;
• str2= str1.Clone(); }

public static unsafe string Copy(string str)


{
if (str == null)
{
throw new ArgumentNullException("str");
}
int length = str.Length; All are same w.r.t functionality.
string str2 = FastAllocateString(length);
fixed (char* chRef = &str2.m_firstChar)
{
fixed (char* chRef2 = &str.m_firstChar)
{
wstrcpy(chRef, chRef2, length);
}
}
return str2;
How to check reference
static void Main(string[] args)
{
string str2 = "Pakistan";
string str1;
str1 = str2;
if (!ReferenceEquals(str1, str2))
Console.WriteLine("str1 = str2, Not Same!");
else
Console.WriteLine("str1 = str2, Same");
str1 = String.Copy(str2);
if (!ReferenceEquals(str1, str2))
Console.WriteLine("String.Copy, Not Same!");
else
Console.WriteLine("String.Copy, Same");
str1 = str2.Clone().ToString();
if (!ReferenceEquals(str1, str2))
Console.WriteLine("str2.Clone(), Not Same!");
else
Console.WriteLine("str2.Clone(), Same");
}
String.ToUpper()
• string VName = “visual programming";
• VName = VName.ToUpper();
• Console.WriteLine(VName);
String.ToLower()
• string VName = “VISUAL PROGRAMMING";
• VName = VName.ToLower();
• Console.WriteLine(VName);
String.Trim()
• string Name = " Trim function removes extra spaces from the
beginning and the ending  ";
• Name = Name.Trim();
• Console.WriteLine(Name);
String.Contains()
• string str1 = "Trim function removes extra spaces from the
beginning and the ending";
• bool result = str1.Contains("removes");
• Console.WriteLine(result); //// True
String.ToCharArray()
• string str1 = "Convert a string to array of character";
• char[] charArray = str1.ToCharArray();
• foreach(char c in charArray)
• {
• Console.WriteLine(c);
•}
String.Substring()
• string str1 = "substring method returns substring of a string.";
• string sliced =  str1.Substring(0, 8);  
• Console.WriteLine(myName);   //// substring
String.StartsWith()
• string str1 = "method checks whether the first character of a string is
same as specified character.";
• bool check =  str1.StartsWith("me");   
• Console.WriteLine(check);   //// True
String.EndsWith()
• Same
String.Split()
• string myName = “It-splits-the-string-on-the-supplied-value-It-return-
the-array-of-string";
• string[] breakMysentence = myName.Split('-');
• foreach (string data in breakMysentence)
• Console.Write(data); //// It splits the string on the supplied value It
return the array of string
Arrays of strings is an array of
arrays of characters.
IndexOf()
• It returns the index position of the first occurrence of a particular
character as an integer value.

• String str1 = "String Functions";


• int index = str1.IndexOf('t');
• Console.WriteLine(index); //// 1
IndexOf()

• String str1 = "String and Functions";


• int index = str1.IndexOf("and");
• Console.WriteLine(index); //// 7
LastIndexOf()
• Same
Insert()
• Insert() method is used to insert the particular string at a specified
index number.

• string str1 = "String Functions";


• string str2 = str1.Insert(7, "and");
• Console.WriteLine(str2); //// String and Functions
Other methods of Strings C#
• CompareTo()
• GetHashCode()
• GetType()
• GetTypeCode()
• Replace()

You might also like