The String Concat()
method concatenates (joins) strings and returns them.
Example
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 = "Ice";
string str1 = " cream";
// concatenates str0 and str1
string result = String.Concat(str0 + str1);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
// Output: Ice cream
Concat() Syntax
The syntax of the String Concat()
method is:
String.Concat(string str0, string str1)
Here, Concat()
is a method of class String
.
Concat() Parameters
The Concat()
method takes the following parameters:
- str0 - first string to concatenate
- str1 - second string to concatenate
Concat() Return Value
The Concat()
method returns:
- a string which is the concatenation of str0 and str1
Example 1: C# String Concat()
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 ="ice";
string str1 = "cream";
string result;
// concatenates str0 and str1
result = String.Concat(str0, str1);
Console.WriteLine(result);
// concatenates str0 and str1
result = String.Concat(str1, str0);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Output
icecream creamice
In the above example,
String.Concat(str0, str1);
joins str1 to the end of str0String.Concat(str1, str0);
joins str0 to the end of str1
Example 2: Concat() - Concatenate Three Strings
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 = "Everyone";
string str1 = " loves";
string str2 = " ice cream";
// concatenates str0, str1, and str2
string result = String.Concat(str0, str1, str2);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Output
Everyone loves ice cream
In the above example, the Concat()
method joins three strings.
Example 3: Concat() - Array Elements
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string[] str = {"Chocolate", " Ice cream", " Vanilla"};
// concatenates string array
string result = String.Concat(str);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Output
Chocolate Ice cream Vanilla
In the above example, the Concat()
method joins the elements in array str.