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"; // hi, all
- string b = @" hi, all "; // hi, all
- string c = " hi \t all "; // hi all
- string d = @" hi \t all "; // hi \t all
- string e = "Lily said \" hi \" today"; // Lily said " hi " today
- string f = @" Lily said ""hi"" today "; // Lily said "hi" today
- string g = "\\\\server\\folder\\file.txt"; // \\server\ folder \file.txt
- string h = @"\\server\ folder \file.txt"; // \\server\ folder \file.txt
Example:
- class MyClass
- {
- static void Main()
- {
- object obj = "Komal";
- object obj1 = "Komal";
- System.Console.WriteLine(obj == obj1);
- }
- }
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:This example will result in $45676. See, no need of writing extra line of code.
- int totalValue = 45676;
- string Currency=string.Format(rgi,"{0:c}", totalValue);
- Console.WriteLine(“Currency”);
- Decimal:
a.
This example will result in 456.57 as it will round the decimal value upto 2 digits.- float totalValue = 456.56676;
- string decimal=string.Format(rgi,"{0:0.00}", totalValue);
- Console.WriteLine(“decimal”);
b.
This example will result in 456.5668 as it will round the decimal value upto 4 digits.- float totalValue = 456.56676;
- string decimal=string.Format(rgi,"{0:4}", totalValue);
- Console.WriteLine(“decimal”);
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:This example will result in 56% as it will add decimal at the end.
- Float totalValue = 56;
- string percentage =string.Format(rgi,"{0:p}", totalValue);
- Console.WriteLine(“percentage”);
- Phone: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.
- int totalValue = 1234567890;
- string number =string.Format(rgi,"{0:(###)###-####}", totalValue);
- Console.WriteLine(“number”);
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.This example will give output as: MYSTRING as this function changes the string into uppercase.
- string value1 = "MyString";
- string upper1 = value1.ToUpper();
- Console.WriteLine(upper1);
- ToLower(): This function converts the string into full small letters. It changes the string into lowercase.This example will give output as: mystring as this function changes the string into lowercase.
- string value1 = "MyString";
- string upper1 = value1.ToLower();
- Console.WriteLine(upper1);
- 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.This example will replace all the values with whitespaces in Mevalue with underscores in it.
- Myvalue= Myvalue.Replace(“ ”,”_”);
- 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.This example will result in Hi as it will remove the characters from end which are specified in the array.
- string MyString = "Hi Coders!";
- char[] MyChar = {‘C’','o','d','r','e','s','! '};
- string NewString = MyString.TrimEnd(MyChar);
- Console.WriteLine(NewString);
- TrimStart(): This helps in removing characters specified in an array of characters from the start of a string.This example will result in Coders as it will remove the characters from start which are specified in the array.
- string MyString = " Hi Coders!";
- char[] MyChar = {'H','i',' ' };
- string NewString = MyString.TrimStart(MyChar);
- Console.WriteLine(NewString);
- Remove(): This helps in removing specified number of characters from a mentioned index position in a string.This example will result Hello People as it will remove the Beautiful from the string.
- string MyString = "Hello Beautiful People!";
- Console.WriteLine(MyString.Remove(5,10));
- Length: This helps in getting the number of characters present in the current string object.
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.- string MyString = "Coders";
- Console.WriteLine(" The length of '{0}' is {1}", MyString, MyString.Length);
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.

Dharmendra Kumar PanditPosted Jan 25, 2019, 6:17 AM
Very helpful article.
sherwin kuizonPosted Aug 22, 2018, 2:51 AM
Thank you very Much