In the previous post, we have discussed String in C#. In this post, we will introduce a brief summary of string concatenation in C#.
C# represents "string" as a collection of characters surrounded by double quotes " ".
What is concatenation?
Concatenation is appending one string to the end of another string by using these methods
- The + operator.
- String Interpolation.
- String.Concate().
- String.Join().
- String.Format().
- StringBuilder.Append().
The + operator
- Concatenate one or more string.
- No run-time concatenation occurs.
- Concatenation occurs at compile time.
Ex:
string userName ="Hamza";
string greetings= "Hello " + userName +" today.";
Console.WriteLine(greetings); // the output will be hello Hamza today.
String Interpolation
- Concatenate variables as a part of a string.
- String interpolation starts with a ‘$’ symbol and code variables are within a bracket {}.
- String interpolation provides a more readable and convenient syntax to create formatted strings.
Ex:
string userName ="Hamza";
Console.WriteLine($"Hello {userName } today."); // the output will be Hello Hamza today.
String.Concate()
- String.Concat() is used to concatenate one or more instance of string, these instances can also be objects and arrays.
- It concatenates the string close to each other without a separator between each value, you should specify it.
Ex:
string firstName="Hamza ";
string lastName="Mohammed";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);// the output will be Hamza Mohammed
You can also use String.Concat() to concatenate array values as shown below:
Ex:
string[] colors= { "red","yellow","black"};
string arrayStr = string.Concat(colors);
Console.WriteLine(arrayStr); // the output will be redyellowblack
String.Join()
- String.Join() is a wonderful option to join strings from a collection using a separator.
- You can use it instead of String.Concat() method if you need to show elements separated by a delimiter.
Ex:
string[] colors= { "red","yellow","black"};
string arrayStr = string.Join(" ",colors);
Console.WriteLine(arrayStr); // the output will be red yellow black
String.Format()
- String.Format() Converts the value of objects to strings based on the formats specified and inserts them into another string.
- Used to format strings into specific formats.
- We can insert one or more objects and expressions in a string at a specified position using the String.Format() method.
- the index in the format starts from 0.
- It has 8 overloaded formats.
Ex:
In this example, you can insert one or multiple objects in a string as shown below.
string firstName="Hamza";
string lastName="Mohammed";
string sformated = String.Format("Hello {0}, your last name is {1}.", firstName,lastName);
Console.WriteLine(sformated );// the output will be Hello Hamza your last name is Mohammed.
StringBuilder.Append()
- Appends the string representation of a specified object to this instance.
- used to modify strings without creating new string objects.
- It is recommended in many string concatenations.
- StringBuilder class is defined in the System.Text namespace.
EX:
System.Text.StringBuilder builder = new System.Text.StringBuilder("Hello Reader");
builder.Append(", ");
builder.Append("this is our article about string concatenation");
Console.WriteLine(builder);// the output will be Hello Reader, this is our article about string concatenation
Conclusion
In this post, we have learned the string concatenation methods in c#.
See Also