String in C#

String :

  • We all know what is String in programming and they are enclosed between the double quotes " " in most of the programming languages. The String may include any characters, numbers, and any special character we can include them in the String.
using System;

class Demo
{
 public static void Main()
 {
    string fav = "Ironman";
    String name = "Tony Stark";
    String power = new String("Intelligence");

    // GetType() is used to find type of variable.
    Console.WriteLine(fav.GetType()); 
    Console.WriteLine(name.GetType());
    Console.WriteLine(power.GetType());
 }
}
/*
Output : 
System.String
System.String
System.String
*/

Note: The String variable declared using all three gives the same String class object only.

Escape character in String \ :

  • In programming we use String in many places some times we need to show/use the double quotes inside a message we want to print on console. but we can't do this because of the special property of the double-quotes.
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello "RDJ""); //Gives error
        }
    }
  • In the above code the "Hello " and " are considered two separate strings because as we know string that is enclosed between two double quotes and RDJ will not be considered as a string because it is not inside quotes.

  • Whenever we want to remove the special meaning of any character we can use the escape character \. As shown in the below program.

class Program
    {
        static void Main(string[] args)
        {
            string name = "RDJ";

            Console.WriteLine("Hello \"RDJ\"");
        }
    }
/*
Output :
Hello "RDJ"
*/

Note: If we want to remove the special meaning of any special character we need to add the escape character that is a backslash (\) with the special character as shown in the above code example.

Verbatim String @ :

  • The dictionary meaning of the word verbatim is in the same words as were used originally. We call also call the verbatim string a multiline string. If we want to return any lines however it is written then we need to use this verbatim string.
class Program
    {
        static void Main(string[] args)
        {
            string sentence = @"This is a verbatim string this has      spaces          tabs 
            and this also has the next line";

            Console.WriteLine(sentence);
        }
    }
/*
This is a verbatim string this has      spaces          tabs
            and this also has the next line
*/

Note: The above verbatim string will print with all the space and tabs and next line. To write verbatim string just add the @ symbol in front of the string it will be considered as a verbatim string.

String Interpolation $:

  • String interpolation is nothing but string formating. This is new compared to other old formatting systems in the string.

  • If we don't use the string formating we need to use the string concatenation which may not properly format the string and we need to format the string with space by typing in the program explicitly. Like in the below program.

class Program
    {
        static void Main(string[] args)
        {
            string userName = "Robert";

            Console.WriteLine("Hello"+userName);

            Console.WriteLine("Hello "+userName);
        }
    }
/*
Output :
HelloRobert
Hello Robert
*/
  • As you can see we need to concatenate space to string to format the welcome message. The above example is we are concatenating only two strings we can add spaces but when we are concatenating more strings then the code will not be readable and understandable. To make the code look clean we can use the String interpolation just by adding the $ sign in front of the string to tell the compiler we are doing string interpolation As shown below.
class Program
    {
        static void Main(string[] args)
        {
            string userName = "Robert";

            Console.WriteLine($"Hello {userName}");
        }
    }
/*
Output :
Hello Robert
*/
  • Compared to the string concatenation the above String interpolation formatted string looks clean to read.