Advertisement



< Prev
Next >



C# String Concat() method




In C#, the Concat() method of String class is used to concat two or more String objects or the string representation of two or more instances of the Object class and returns the concatenated String value.




Some overloads of Concat() method


All the overloads of the Concat() method are static in nature.

Methods Description
Concat(String, String)
This method concatenates the two instances of String.

Concat(String, String, String)
This method concatenates the three instances of String.

Concat(String, String, String, String)
This method concatenates the four instances of String.

Concat(Object, Object)
This method concatenates the two instances of the Object class.

Concat(Object, Object, Object)
This method concatenates the three instances of the Object class.

Concat(Object, Object, Object, Object)
This method concatenates the four instances of the Object class.

Concat(String[ ])
This method concatenates the elements of a String array.

Concat(Object[ ])
This method concatenates the string representation of the elements in a Object array.

Concat(Object)
This method creates the string representation of an instance of the Object class.






Calling the Concat() method to concatenate the String objects


In the upcoming code, we are calling the different versions of the Concat() method to concatenate the individual String object and even the String objects in a String array.

//C# Program to concatenate values in two or more String Objects.

using System;

class StringCon1
{
public static void Main()
{
	//Creating a char array
	char[] charr = {'H', 'e', 'l', 'l', 'o'};

	//Creating the first String object from a char[] array.
	String str1 = new String(charr);

	//Second String object created by directly assigning String literal
	String str2 = "World";		

	//Third String object.
	String str3 = "There";	
	
	//Fourth String object.
	String str4 = "You";

	//A String array
	String[] strarr = {"Best", " ", "of", " ", "luck!"};

	
	//Printing the values of 4 Strings.
	Console.WriteLine("First  String value : "+ str1);
	Console.WriteLine("Second String value : "+ str2);
	Console.WriteLine("Third String value : "+ str3);
	Console.WriteLine("Fourth String value : "+ str4);
	

	//Printing the elements of a String array
	Console.WriteLine("Elements of a String array:");
	foreach(String str in strarr)
		Console.WriteLine("'" + str + "'"  + " " );


	Console.WriteLine("Concatenating the first two strings : "+ String.Concat(str1, str2));
	Console.WriteLine("Concatenating the first three strings : "+ String.Concat(str1, str2, str3));
	Console.WriteLine("Concatenating the four strings : "+ String.Concat(str1, str2, str3, str4));
	Console.WriteLine("Concatenating the elements of a String array : "+ String.Concat(strarr));
}
}


Output is -


First  String value : Hello
Second String value : World
Third String value : There
Fourth String value : You
Elements of a String array:
'Best'
' '
'of'
' '
'luck!'
Concatenating the first two strings : HelloWorld
Concatenating the first three strings : HelloWorldThere
Concatenating the four strings : HelloWorldThereYou
Concatenating the elements of a String array : Best of luck!





The Concat() method also returns the string representation of the object of Object class


In the upcoming code, we are calling an overload version of the Concat() method which returns the string representation of an object of Object class.

//C# Concat() method returns the string representation of an object of the Object class.
//Note: The Object class is the mother of all classes in C#

using System;

class A
{
public static void Main()
{
	//Creating the first object by directly assigning a String literal
	//which will be boxed to an object of Object type.
	Object ob1 = "Hey";


	//Creating the second object by assigning it an object of class A	
	//which will be boxed to an object of Object type.
	Object ob2 = new A();		


	//Third String object by assigning it an int value
	//which will be boxed to an object of Object type.
	Object ob3 = 12345;	
	

	//Fourth String object by assigning it an char value
	//which will be boxed to an object of Object type.
	Object ob4 = 'X';

	
	//Printing the string representation of 4 objects of the Object class.
	Console.WriteLine("String representation of the first Object  : "+ String.Concat(ob1));
	Console.WriteLine("String representation of the second Object  : "+ String.Concat(ob2));;
	Console.WriteLine("String representation of the third Object  : "+ String.Concat(ob3));
	Console.WriteLine("String representation of the fourth Object  : "+ String.Concat(ob4));
}
}


Output is -


String representation of the first Object : Hey
String representation of the second Object : A
String representation of the third Object : 12345
String representation of the fourth Object : X



Advertisement




Calling the Concat() method to concatenate the objects of Object class.


In the upcoming code, we are calling the different versions of the Concat() method to concatenate the individual objects of Object class and even the objects in am Object array.

//C# Program to concatenate the string representation of two or more objects of the Object class.
//Note: The Object class is the mother of all classes in C#

using System;

class A
{
public static void Main()
{
	//Creating the first object by directly assigning a String literal
	//which will be boxed to an object of Object type.
	Object ob1 = "Hey";


	//Creating the second object by assigning it an object of class A	
	//which will be boxed to an object of Object type.
	Object ob2 = new A();		


	//Third String object by assigning it an int value
	//which will be boxed to an object of Object type.
	Object ob3 = 12345;	
	

	//Fourth String object by assigning it an char value
	//which will be boxed to an object of Object type.
	Object ob4 = 'X';


	//An Object array
	Object[] objarr = {"321", 'a', "Always think positive", 2.45f,  19.9};

	
	//Printing the values of 4 Object values.
	Console.WriteLine("First Object value : "+ ob1);
	Console.WriteLine("Second Object value : "+ ob2);
	Console.WriteLine("Third Object value : "+ ob3);
	Console.WriteLine("Fourth Object value : "+ ob4);
	

	//Printing the elements of an Object array
	Console.WriteLine("Elements of an Object array:");
	foreach(Object ob in objarr)
		Console.WriteLine("'" + ob + "'"  + " " );


	Console.WriteLine("Concatenating the first two objects : "+ String.Concat(ob1, ob2));
	Console.WriteLine("Concatenating the first three objects : "+ String.Concat(ob1, ob2, ob3));
	Console.WriteLine("Concatenating the four objects : "+ String.Concat(ob1, ob2, ob3, ob4));
	Console.WriteLine("Concatenating the elements of an Object array : "+ String.Concat(objarr));
}
}


Output is -


First Object value : Hey
Second Object value : A
Third Object value : 12345
Fourth Object value : X
Elements of an Object array:
'321'
'a'
'Always think positive'
'2.45'
'19.9'
Concatenating the first two objects : HeyA
Concatenating the first three objects : HeyA12345
Concatenating the four objects : HeyA12345X
Concatenating the elements of an Object array : 321aAlways think positive2.4519.9





Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore, if we want, we can make the resulting concatenated String assign to any of the existing String objects(but in this case, the original String will be lost).

// C# Strings are immutable but reference variables are mutable.

using System;

class StringCon2
{
public static void Main()
{
	//Creating two String objects.
	String str1 = "South";
	String str2 = " Africa";

	//Printing the String objects
	Console.WriteLine("First String value is: "+ str1);
	Console.WriteLine("Second String value is: "+ str2);

	//Calling the Concat() method to concatenate two String objects
	//Assigning the resulting String to the first String object
	str1 = String.Concat(str1,str2);

	Console.WriteLine("Value in the first String after concatenation with the second String : "+ str1);
}
}


Output is :


First  String value is: South
Second String value is:  Africa
Value in the first String after concatenation with the second String : South Africa





Please share this article -





< Prev
<Next >
< C# String Comparison with ==
C# Trim() method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement