Different Ways to Concatenate Strings in C#
Nov 27, 2020 06:10 0 Comments C# (C-Sharp) PARTH

  Different Ways to Concatenate Strings in C#


1. Using the ‘+’ operator:


     string concatenatedOne = "This " + "is " + "a " + "concatenated " + "string.";



2. Using the static method called Concat where you can pass in an IEnumerable of string


IEnumerable strings = new List() {"This ",  "is ", "a ", "concatenated ", "string." };

string concatenatedTwo = string.Concat(strings);



3. Using the static method called Join. The first parameter in the Join method is the separator which will be added in between the individual string elements


string concatenatedThree = string.Join(" ", "This", "is", "a", "concatenated", "string.");



4. Using the Append method in case of String Builder


StringBuilder sb = new StringBuilder();

sb.Append("This ").Append("is ").Append("a ").Append("concatenated ").Append("string.");

string concatenatedFour = sb.ToString();



5. Using the string.Format method where you can supply a format as the first parameter and a params array of the elements that will be substituted into the format


string concatenatedFive = string.Format("{0} {1} {2} {3} {4}", "This", "is", "a", "concatenated", "string.");




6. Using the $ operator


public class Organization


    public string Name { get; }


    public Organization (string employeeName)


        Name = employeeName;



    public string GetNickName()


        return Name.ToUpper();



      Organization employee = new Organization ("Meena");


string concatenatedSix = $"The employee’s name is {employee.Name} and has a nick name of {employee.GetNickName()}.";

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.

Let`s Connect