C# | String Concat with examples | Set-2
Last Updated :
01 Feb, 2019
String.Concat Method is used to concatenate one or more instances of
String or the
String representations of the values of one or more instances of Object. It always returns a concatenated string.
This method can be overloaded by passing different types and number of parameters to it. There are total
11 methods in the overload list of the Concat method in which first 3 are discussed in
Set-1 and remaining are discussed in
Set-2, Set-3, and Set-4.
4. Concat(String, String)
This method is used to concatenate two different instances of the string. You can also use Concatenation operator(
+)to concatenate strings.
Note: If an array contains a null object, then the Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB);
Parameters:
strA: First string to concatenate.
strB: Second string to concatenate.
Return Value: The return type of this method is
System.String. This method returns a string as an result of concatenation of two strings, i.e.
strA, and
strB.
Example:
CSharp
// C# program to illustrate the use
// of Concat(String, String ) Method
using System;
public class GFG {
// Main Method
static public void Main()
{
string strA = "Hello! ";
string strB = "Geeks.";
string str;
// print all strings
Console.WriteLine("String A is: {0}", strA);
Console.WriteLine("String B is: {0}", strB);
// Concatenate two different strings
// into a single String
// using Concat(String, String ) Method
str = String.Concat(strA, strB);
Console.WriteLine("Concatenated string is: {0}", str);
}
}
Output:
String A is: Hello!
String B is: Geeks.
Concatenated string is: Hello! Geeks.
5. Concat(String, String, String)
This method is used to concatenate three different string into a single string. You can also use Concatenation operator(
+)to concatenate strings.
Note: If an array contains a null object, then Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB, string strC);
Parameters:
strA: First string to concatenate.
strB: Second string to concatenate.
strC: Third string to concatenate.
Return Value:The return type of this method is
System.String. This method returns a string which is created from the concatenation of three strings, i.e.
strA,
strB, and
strC.
Example:
CSharp
// C# program to illustrate the
// Concat(String, String, String) Method
using System;
class GFG {
// Main Method
static public void Main()
{
string strA = "Welcome ";
string strB = "to ";
string strC = "GFG. ";
string str;
// print all strings
Console.WriteLine("String A is: {0}", strA);
Console.WriteLine("String B is: {0}", strB);
Console.WriteLine("String C is: {0}", strC);
// Concatenate three different strings
// into a single String using the
// Concat(String, String, String ) Method
str = String.Concat(strA, strB, strC);
Console.WriteLine("Concatenated string is: {0}", str);
}
}
Output:
String A is: Welcome
String B is: to
String C is: GFG.
Concatenated string is: Welcome to GFG.
6. Concat(String[])
This method is used to concatenates the elements of a specified String array.
Syntax:
public static string Concat (params string[] items);
Here,
items are the array of string instance.
Return Value: The return type of this method is
System.String. This method returns the concatenated elements of items.
Exceptions:
- If the value of the given string items is null then this method will give ArgumentNullException.
- If the array is out of memory, then this method will give OutOfMemoryException .
Example:
CSharp
// C# program to illustrate the
// Concat(string[]) Method
using System;
class GFG {
// Main method
public static void Main()
{
// array
string[] strA = {"Hey, ", "This ","is ", "C# ", "Tutorial."};
// print elements of array
foreach(string elements in strA)
{
Console.WriteLine("Elements of strA array : {0}", elements);
}
// concatenate the element of array
// into single string
// using Concat(string[]) Method
Console.WriteLine("After Concatenation: {0}",
string.Concat(strA));
}
}
Output:
Elements of strA array : Hey,
Elements of strA array : This
Elements of strA array : is
Elements of strA array : C#
Elements of strA array : Tutorial.
After Concatenation: Hey, This is C# Tutorial.
Next : Set 3
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.concat?view=netframework-4.7.2
Similar Reads
C# | String Concat with examples | Set-1
String.Concat Method is used to concatenate one or more instances of String or the String representations of the values of one or more instances of Object. It always returns a concatenated string. This method can be overloaded by passing different types and number of parameters to it. There are tota
4 min read
C# | String Concat with examples | Set-3
String.Concat Method is used to concatenate one or more instances of String or the String representations of the values of one or more instances of Object. It always returns a concatenated string. This method can be overloaded by passing different types and number of parameters to it. There are tota
3 min read
C# String Join Method | Set - 2
The C# Join() method is a method that is present in the String class. This method is used to concatenate or combine different kinds of collections, such as an array, list or set. With the specified separator between each member or element. Key Features:Concatenation: The Join() method is used to com
4 min read
C# String Join() Method | Set - 1
In C#, Join() is a string method. This method is used to concatenate the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it.Concatenation: This method is use
5 min read
C# Verbatim String Literal - @
In C#, a verbatim string is created using a special symbol @. The symbol(@) is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell th
5 min read
C# StringBuilder
StringBuilder is a Dynamic Object. It doesnât create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.A String object is immutable, i.e. a String cannot be changed once created. To avoid string replacing, appending, removing or inserting
4 min read
How to Concatenate Multiple Strings in C?
In C, concatenating strings means joining two or more strings end-to-end to form a new string. In this article, we will learn how to concatenate multiple strings in C. Example: Input:char str1[50] = "Hello";char str2[50] = " geeksforgeeks";Output:Hello geeksforgeeks!Concatenating Strings in CTo conc
1 min read
Concatenating Two Strings in C
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example:C#include <stdio.h> #i
2 min read
Shell Script to Concatenate Two Strings
String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other. The examples below show some shell scripts that can be used to concatenate strings. E
3 min read
Copy N Characters from One String to Another Without strncat()
In C, strings are the sequence of characters that are used to represent the textual data. Two strings can be easily concatenated or joined using the strncat() function of the C standard library up to n characters. In this article, we will learn how to concatenate the first n characters from one stri
2 min read