There are different meanings of special characters in C#. But what if I need to add that character in as a literal in our code? For using these special characters, we use another character. Confused? The chart provided below will help you to understand what I am trying to convey.
Escape Characters | Explanation |
\' | For printing single quote. |
\" | For printing double quote. |
\\ | For printing back slash. |
\\\\ | For printing double back slash. |
\? | For printing question mark. |
\n | For going to new line. |
\b | For backspacing. |
\0 | For putting a null value. |
\a | For putting an alert. |
\f | For form feed |
\r | For carriage return. |
\t | For horizontal tab. |
\v | For vertical tab. |
"" | For providing the data inside the qoutes as it is. |
@"" | For providing the data inside the @ qoutes as it is without caring of any escape character in between. |
Example:
- string a = "hi, all";
- string b = @" hi, all ";
- string c = " hi \t all ";
- string d = @" hi \t all ";
- string e = "Lily said \" hi \" today";
- string f = @" Lily said ""hi"" today ";
- string g = "\\\\server\\folder\\file.txt";
- string h = @"\\server\ folder \file.txt";
Similarly, True & False are boolean values which we can’t display directly. What if we want to display true or false depending on the value of the result not by using Console.Writline(“True”)? Here is an example which will show true or false depending on the result.
Example:
- class MyClass
- {
- static void Main()
- {
- object obj = "Komal";
- object obj1 = "Komal";
- System.Console.WriteLine(obj == obj1);
- }
- }
This example will give output: True
As obj & obj1 holds equal values & accordingly they get matched, hence it results in True.
In addition to these escape characters, I will also tell you about how to add a particular sign before any value. Piece of cake right? Just kidding. Many times we come across situations where we need to add a $ sign at the value of total amount or % while showing the percentage of the total marks, these are few examples which we will sort out without writing any extra line of code. Lets look into this:
- Currency:
- int totalValue = 45676;
- string Currency=string.Format(rgi,"{0:c}", totalValue);
- Console.WriteLine(“Currency”);
This example will result in $45676. See, no need of writing extra line of code.
- Decimal:
a.
- float totalValue = 456.56676;
- string decimal=string.Format(rgi,"{0:0.00}", totalValue);
- Console.WriteLine(“decimal”);
This example will result in 456.57 as it will round the decimal value upto 2 digits.
b.
- float totalValue = 456.56676;
- string decimal=string.Format(rgi,"{0:4}", totalValue);
- Console.WriteLine(“decimal”);
This example will result in 456.5668 as it will round the decimal value upto 4 digits.
Similarly, we can do it upto n digits replacing the number 4 with any number you want upto which decimal point should appear from R.H.S.
- Percent:
- Float totalValue = 56;
- string percentage =string.Format(rgi,"{0:p}", totalValue);
- Console.WriteLine(“percentage”);
This example will result in 56% as it will add decimal at the end.
- Phone:
- int totalValue = 1234567890;
- string number =string.Format(rgi,"{0:(###)###-####}", totalValue);
- Console.WriteLine(“number”);
This example will result in 1234567890 as it will verify the number as phone number by checking if it is 10 digit number or not. If it is a 10 digit number, it will accept it as a phone number else it will show error.
Next, we will see how to change a string value in full capital letters or small letters, check the size of the string etc. So, lets dig in,
- ToUpper(): This function converts the string into full Capital letters. It changes the string into uppercase.
- string value1 = "MyString";
- string upper1 = value1.ToUpper();
- Console.WriteLine(upper1);
This example will give output as: MYSTRING as this function changes the string into uppercase.
- ToLower(): This function converts the string into full small letters. It changes the string into lowercase.
- string value1 = "MyString";
- string upper1 = value1.ToLower();
- Console.WriteLine(upper1);
This example will give output as: mystring as this function changes the string into lowercase.
- Replace(): This helps in replacing some value in your text with another. Here I try to replace my whitespaces with underscores as my code demands no whitespaces.
- Myvalue= Myvalue.Replace(“ ”,”_”);
This example will replace all the values with whitespaces in Mevalue with underscores in it.
- Trim(): This helps in removing the L.H.S. & R.H.S. whitespaces in the text if available.
- string name = “ komal “;
- Console.WriteLine("My name is " + name.Trim());
- TrimEnd(): This helps in removing characters specified in an array of characters from the end of a string.
- string MyString = "Hi Coders!";
- char[] MyChar = {‘C’','o','d','r','e','s','! '};
- string NewString = MyString.TrimEnd(MyChar);
- Console.WriteLine(NewString);
This example will result in Hi as it will remove the characters from end which are specified in the array.
- TrimStart(): This helps in removing characters specified in an array of characters from the start of a string.
- string MyString = " Hi Coders!";
- char[] MyChar = {'H','i',' ' };
- string NewString = MyString.TrimStart(MyChar);
- Console.WriteLine(NewString);
This example will result in Coders as it will remove the characters from start which are specified in the array.
- Remove(): This helps in removing specified number of characters from a mentioned index position in a string.
- string MyString = "Hello Beautiful People!";
- Console.WriteLine(MyString.Remove(5,10));
This example will result Hello People as it will remove the Beautiful from the string.
- Length: This helps in getting the number of characters present in the current string object.
- string MyString = "Coders";
- Console.WriteLine(" The length of '{0}' is {1}", MyString, MyString.Length);
This example will result: The length of Coders is 6. As it will return the count of the number of characters present in the string.
I hope this piece of information will help you a little to save time from writing extra lines of code.
Please do comment and send necessary feedbacks. Hope to write soon on some other topic.