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:
using System;
public class GFG {
static public void Main()
{
string strA = "Hello! " ;
string strB = "Geeks." ;
string str;
Console.WriteLine( "String A is: {0}" , strA);
Console.WriteLine( "String B is: {0}" , strB);
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:
using System;
class GFG {
static public void Main()
{
string strA = "Welcome " ;
string strB = "to " ;
string strC = "GFG. " ;
string str;
Console.WriteLine( "String A is: {0}" , strA);
Console.WriteLine( "String B is: {0}" , strB);
Console.WriteLine( "String C is: {0}" , strC);
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:
using System;
class GFG {
public static void Main()
{
string [] strA = { "Hey, " , "This " , "is " , "C# " , "Tutorial." };
foreach ( string elements in strA)
{
Console.WriteLine( "Elements of strA array : {0}" , elements);
}
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-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 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 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 co
4 min read
C# Program For Counting Lines in a String
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. A string is the representation of the text. In this article, we will create a C# program that will count the lines present in the given string. Example: Input : he
6 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
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: [GFGTABS] C #include <s
3 min read
How to Reverse a String in C?
In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu
2 min read
C Program to Concatenate Two Strings Without Using strcat
String concatenation is the process of appending one string to the end of another. C language provides strcat() library function to concatenate string but in this article, we will learn how to concatenate two strings without using strcat() in C. The most straightforward method to concatenate two str
3 min read
Reverse String in C
In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C. The most straightforward method to reverse string is by using two pointers
3 min read
C Program to Concatenate Two Strings Using a Pointer
Concatenating two strings means appending one string at the end of another string. While the standard library provides strcat() for concatenation, this article will demonstrate how to concatenate two strings using pointers. To concatenate two strings using pointers, traverse the first string to its
1 min read