Part 24. String Concate & String Interpolation 


String Concate :

Here 6 ways to concatenate strings in C#. 

  1. Using + operator
  2. String Interpolation
  3. String.Concatenate() method
  4. String.Join() method
  5. String.Format() method
  6. StringBuilder.Append() method 

1. Concatenate String + Operator

The simplest method of adding two strings is using + or += operators. Concatenation is the process of appending one string to the another string. You concatenate strings by using the concate , + operator.

Example 1 :

The concatenation of two strings.
class Concate
{
  public static void main(string[] args)
  {
            string fname = "chandan";
            string lname = "pandey";
            str = fname +" "+lname;
            Console.WriteLine(str);
            Console.ReadLine();
  }
}
Output
chandan pandey

Example 2 :

class Concate
{
  public static void main(string[] args)
  {
            string Name = "Chandan";
            string dateString = DateTime.Today.ToShortDateString();
            // Use the + and += operators for one-time concatenations.
            string str = "Hello " + Name + ". Today is " + dateString + ".";
            System.Console.WriteLine(str);
            str += " How are you today?";
            Console.WriteLine(str);

            Console.ReadLine();
    }
}
Output:
Hello Chandan. Today is 28-02-2020.

Hello Chandan. Today is 28-02-2020. How are you today?


2. String Interpolation 



String interpolation is a method of concatenating, formatting and manipulating strings.An interpolated string returns a string as
 a result.

Syntax :

{<interpolatedExpression>[,<alignment>][:<formatString>]}

 String interpolation starts with a ‘$’ symbol and expressions are defined within a bracket {}.

String Interpolation is a feature which is used to embed any valid expression that returns a value into a string.

class Concate

{
  public static void main(string[] args)
  {
            string Name = "Chandan";
            int age = 24;
           // Composit format string                                                                                                Console.WriteLine("Name = {0}, age = {1}", name, age);  

           var s1   = $"{Name} is {age} years old.";  

         Console.WriteLine(s1);  
           Console.ReadLine();
    }
}


// String interpolation 
string str = $"Hi {name}, Today is {date.DayOfWeek} and it's {date:HH:mm} now.";
Console.WriteLine(strIntr);

C# Multiline String Interpolation

String Interpolation will support multiline.

Example of implementing a multiline, The string must starts with $ character followed by @ character.

class Concate
{
  public static void main(string[] args)
  {
     string name = "Chandan";
     string location = "Gurgaon;
     DateTime date = DateTime.Now;

     string msg = $@"Hi {name}, Today is {date.DayOfWeek} and it's {date:HH:mm}                                                       now.Welcome to Csharp-tutorial-World Your location is {location}";


    Console.WriteLine(msg);


    Console.ReadLine();
  }
}

The string interpolation performance is almost same as String.Format() method.


1. Why we use string Interpolation ?
String interpolation provides a more readable and convenient syntax to create formatted strings

string name = "Chandan";

var date = DateTime.Now;

// Composite formatting:

Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// Hello, Chandan! Today is Monday, it's 7:40 now. //as per your date output comes.
Note : Composite format is Notice that the string literal is not easy to read. 

Notice that the string interpolation is easy to read. Here is the equivalent with traditional string concatenation.


// String interpolation:

Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Hello, Chandan! Today is Monday, it's 7:40 now. //as per your date output comes.

Some Important Point.

1.  The dollar sign at the start of the literal specifies that the string contains interpolation.

2. It is not always appropriate to leave string literals in code, but when it is appropriate, developers should use string interpolation


Interview Points I'm attending Interview some few question ask by company?


1. What is String Interpolation in C#?


Ans : String Interpolation is a mechanism to concatenate two or more strings together.

                                      In older versions of C#, we were using “+” operator or String. Format method to concatenate strings but in C# 6.0, Microsoft has provided a feature named String Interpolation to concatenate strings.

2. Where did you use String Interpolation ?


Ans : 1. You can place the String where ever you want it

         2. You can use conditions
         3. You can specify space before/after the string.

Example:

class Concate
{
  public static void main(string[] args)
  {
     string name = "Chandan";
     string location = "Gurgaon;
    // Putting String Where you need 
      Console.WriteLine($"{name } {location } is my name and Address!");        

      Console.ReadLine();  
   }
}