C# Basics-Part 5(Working with strings)
Most of the time in our automation project we will end up dealing with strings, that's the reason we will spend some time talking about strings in this lecture.
Before we proceed we will talk about types first.
Types in .Net can be either value or reference type
Value types
They store data directly. example int, decimal etc.
Reference types
They store references to their data. example object, string etc
string
The string is a reference type and is immutable ie contents of strings cannot be changed after a string is created.
We can use the new keyword to create string object when initializing a string with an array of chars
String concatenation
The += operator creates a new string that contains the combined contents.
Regular and Verbatim String Literals
Output:
Regular string: Line1
Line2
Verbatim string: Line1 \r\n Line2
In regular string characters are processed however if you do not want to process the characters in the string then you can use Verbatim string
Handling nulls
Output:
Result1: True
Result2: True
Format Strings
There are multiple ways you can format the strings, check below code
String interpolation was introduced in C# 6.0, achieves the same results as String.Format, however, helps to improve code readability and ease of use.
String Builder
We discussed earlier that strings as immutable ie when we concatenate two strings it will create a new string.
So if you are working on string manipulation where you change or concatenate string multiple times, it better if we can have a mutable string which can be changed without creating a new string.
String Builder class helps us to deal with strings as if they are mutable and allow an operation like concatenation without creating a new string.
Below example code
Here we are creating only one string and appending the contents in the loop. making our code more efficient
Before we proceed we will talk about types first.
Types in .Net can be either value or reference type
Value types
They store data directly. example int, decimal etc.
int i = 100;Here variable i stores the value 100
Reference types
They store references to their data. example object, string etc
string str = "Hello I am amit";Here str variable stores address of memory location which stores the actual "Hello I am amit" string;
string
The string is a reference type and is immutable ie contents of strings cannot be changed after a string is created.
string str = "Hello I am amit"; char[] strLetters = { '1', 'A', 'B' }; string str1 = new string(strLetters);
We can use the new keyword to create string object when initializing a string with an array of chars
String concatenation
string s1 = "String1 "; string s2 = "String2"; s1 += s2; Console.Write(s1);
//Output: String1 String2
The += operator creates a new string that contains the combined contents.
Regular and Verbatim String Literals
string s1 = "Line1 \r\n Line2 "; string s2 = @"Line1 \r\n Line2 "; Console.WriteLine("Regular string: "+s1); Console.WriteLine("Verbatim string: " + s2); Console.Read();
Output:
Regular string: Line1
Line2
Verbatim string: Line1 \r\n Line2
In regular string characters are processed however if you do not want to process the characters in the string then you can use Verbatim string
Handling nulls
string s1 = " "; string s2 = null; var result1 = string.IsNullOrWhiteSpace(s1); var result2 = string.IsNullOrEmpty(s2); Console.WriteLine("Result1: " + result1); Console.WriteLine("Result2: " + result2); Console.Read();
Output:
Result1: True
Result2: True
Format Strings
There are multiple ways you can format the strings, check below code
String interpolation was introduced in C# 6.0, achieves the same results as String.Format, however, helps to improve code readability and ease of use.
String Builder
We discussed earlier that strings as immutable ie when we concatenate two strings it will create a new string.
So if you are working on string manipulation where you change or concatenate string multiple times, it better if we can have a mutable string which can be changed without creating a new string.
String Builder class helps us to deal with strings as if they are mutable and allow an operation like concatenation without creating a new string.
Below example code
StringBuilder stringBuilder = new StringBuilder("Initial String"); for(int i=0;i<10;i++) { stringBuilder.Append(i); stringBuilder.Append("_"); } Console.WriteLine(stringBuilder);
Here we are creating only one string and appending the contents in the loop. making our code more efficient
Comments
Post a Comment