Escape Characters and Some Extra Functions in C#

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:

  1. string a = "hi, all"// hi, all  
  2. string b = @" hi, all "// hi, all  
  3. string c = " hi \t all "// hi all  
  4. string d = @" hi \t all "// hi \t all  
  5. string e = "Lily said \" hi \" today"// Lily said " hi " today  
  6. string f = @" Lily said ""hi"" today "// Lily said "hi" today  
  7. string g = "\\\\server\\folder\\file.txt"// \\server\ folder \file.txt  
  8. string h = @"\\server\ folder \file.txt"// \\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:
  1. class MyClass  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         object obj = "Komal";  
  6.         object obj1 = "Komal";  
  7.         System.Console.WriteLine(obj == obj1);  
  8.     }  
  9. }  
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:
    1. int totalValue = 45676;  
    2. string Currency=string.Format(rgi,"{0:c}", totalValue);  
    3. Console.WriteLine(“Currency”);  
    This example will result in $45676. See, no need of writing extra line of code.

  • Decimal:

    a.
    1. float totalValue = 456.56676;  
    2. string decimal=string.Format(rgi,"{0:0.00}", totalValue);  
    3. Console.WriteLine(“decimal”);  
    This example will result in 456.57 as it will round the decimal value upto 2 digits.

    b.
    1. float totalValue = 456.56676;  
    2. string decimal=string.Format(rgi,"{0:4}", totalValue);  
    3. 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:
    1. Float totalValue = 56;  
    2. string percentage =string.Format(rgi,"{0:p}", totalValue);  
    3. Console.WriteLine(“percentage”);  
    This example will result in 56% as it will add decimal at the end.

  • Phone:
    1. int totalValue = 1234567890;  
    2. string number =string.Format(rgi,"{0:(###)###-####}", totalValue);  
    3. 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.
    1. string value1 = "MyString";  
    2. string upper1 = value1.ToUpper();  
    3. 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.
    1. string value1 = "MyString";  
    2. string upper1 = value1.ToLower();  
    3. 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.
    1. 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.
    1. string name = “ komal “;  
    2. 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.
    1. string MyString = "Hi Coders!";  
    2. char[] MyChar = {‘C’','o','d','r','e','s','! '};  
    3. string NewString = MyString.TrimEnd(MyChar);  
    4. 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.
    1. string MyString = " Hi Coders!";  
    2. char[] MyChar = {'H','i',' ' };  
    3. string NewString = MyString.TrimStart(MyChar);  
    4. 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.
    1. string MyString = "Hello Beautiful People!";  
    2. 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.

    1. string MyString = "Coders";  
    2. 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.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now