-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Strings
- Important String Methods
C-Sharp String
- A string variable contains a collection of characters surrounded by double quotes.
- In C#, a string is a series of characters that is used to represent text. It can be a character or Alphabets, Numbers, Special characters, word or a long passage surrounded with the double quotes “ “
- An array of characters works same as C# string.
- There two ways to declare a string variable in C#. Using System.String class and using string keyword. Both are the same and make no difference.
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
- User can merge/combine two or more C# String fields using the + operator and concatenate using Concat() method. string str1 = “Selenium “; string str2 =”Training ”; Console.WriteLine(str1 + str2 +500); //Selenium Training 500 Console.WriteLine(String.Concat(str1, str2)); //Selenium Training
- IndexOf(): Returns the index of the first occurrence of the specified character/substring within the string. -1 if the specified character/string is not found. string str2 = "studyhard"; Console.WriteLine(str2.IndexOf(‘t’)); //Output: 1 Console.WriteLine(str2.IndexOf(‘0’)); //Output: -1 Console.WriteLine(str2[1]); // Outputs “t“ using string index
- CompareTo(): It compares two strings in the alphabetical order. Result is 0 when the strings are equal; Positive integer in case the first string comes after the second string in the alphabetical order; Negative integer when the first string comes before the second string in the alphabetical order. string s1 = "Amit"; string s2 = “Pradip"; Console.WriteLine(s1.CompareTo(s2));
- Length: Returns the number of characters in a String. string str4=“CSharp"; Console.WriteLine("string length is: "+ str4.length());
- Replace(): Returns a string replacing all the old char or CharSequence to new char or CharSequence. string str5=“Tutorialspoint is a very good website"; Console.WriteLine(str5.Replace(“is", “was")); //Replace phrase Console.WriteLine(str5.Replace('o', 'e')); //Replace char word
String class function - Contd
- ToLower(): Returns string with all uppercase characters changed to lowercase string str4 = "ABCDEFGHIJ"; Console.WriteLine(str4.ToLower()); //Output: abcdefghij
- ToUpper(): Returns string with all lowercase character changed to uppercase string str5 = “selenium"; Console.WriteLine(str5.ToLower()); //Output: SELENIUM
- Trim(): Returns a string from which any leading and trailing whitespaces are being removed string str6 = " helloCSharp "; Console.WriteLine(str6.Trim()); //Output: helloCSharp
- Contains(): Searches the sequence of characters in this string. It returns True if sequence of char values are found in this string or else returns False. 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
String class function - Contd
- Empty: Create a String without anything or zero length. string emptyStr = String.Empty; string emptyStr1 = null; Console.WriteLine(emptyStr); Console.WriteLine(emptyStr1);
- StartsWith(): Checks if this string starts with given prefix. It returns true if this string starts with given prefix or else returns false. string s3=“welcome to CSharp and selenium training session"; Console.WriteLine(s3.StartsWith(“we")); // Output: True
- EndsWith(): Checks if this string ends with given prefix. It returns true if this string ends with given prefix else returns false. string s4=“welcome to CSharp and selenium training session"; Console.WriteLine(s4.EndsWith(“session")); // Output: True Console.WriteLine(s4.EndsWith(“CSharp")); // Output: False
- Substring(): Returns a part of the string. User pass begin index and length in the CSharp substring method where start index is inclusive and length is exclusive. string s5="CSharptraining"; Console.WriteLine(s5.Substring(3)); // Output: arptraining Console.WriteLine(s5.Substring(3, 6)); // Output: arptra
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