• Home
  • Strings and Important String Methods

Strings and Important String Methods

Agenda

C-Sharp String

String Example:

 string s1 = “Welcome to C#”;

char[] ch={‘c’,’h',’a',’r’,’p'};>/code>

String s2=new String(ch);

String s3= “C# String";

string s3 = new string('B', 5); // Output BBBBB


String class function

String class function - Contd

String class function - Contd

 string s1 = "CSharptraining";
Console.WriteLine(s1[1]); // Outputs "H"
String s4 = " welcome to C# ";
string s5 = "zelcome to C#";
Console.WriteLine(s1);
Console.WriteLine(s4);
Console.WriteLine(String.Concat(s1, s4));

char[] ch ={'c','c','h','a','r','p'};
String s2=new String(ch);
Console.WriteLine(s2);

Console.WriteLine(s5.CompareTo(s1));

string s3 = new string('B', 5);

string emptyStr = String.Empty;
string emptyStr1 = null;
String str2 = "studyhard";
Console.WriteLine(str2.IndexOf('t')); //Output: 1
Console.WriteLine(str2.IndexOf('0')); //Output: -1

Console.WriteLine(s3);
Console.WriteLine(emptyStr);
Console.WriteLine(emptyStr1);
Console.WriteLine(s4);

String str3 ="welcome to CSharp and selenium training session";
Console.WriteLine(str3.StartsWith("we")); // Output: True
Console.WriteLine(str3.EndsWith("we"));   // Output: False
Console.WriteLine(str3.EndsWith("session"));   // Output: True

Console.WriteLine(s4.Trim());
Console.WriteLine(s3.Length);
Console.WriteLine(s1.ToLower());
Console.WriteLine(s1.ToUpper());
Console.WriteLine(s1+100+s2+s3);
Console.WriteLine(string.Concat(s1,s2,s3));

Console.WriteLine(s1[2]); // l
Console.WriteLine(s1.IndexOf("o")); // 3
Console.WriteLine(s1.Substring(3));
Console.WriteLine(s1.Substring(3, 6)); //starting from 3 index and then total 6 char
Console.WriteLine(s1.Replace("to", "too")); //Replace pharase
Console.WriteLine(s1.Replace('o', 'e')); //Replace char word

string name = "do you know about selenium webdriver?";
Console.WriteLine(name.Contains("you know")); //Output: True
Console.WriteLine(name.Contains("hello")); //Output: False
Console.WriteLine(name.Contains("you")); //Output: True