In C#, Join() is a string method. This method is used to concatenates 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. There are total 5 methods in the overload list of the Join() method in which 3 are discussed in this article and remaining 2 will be discussed in C# | Join() Method | Set – 2.
String.Join(String, Obj [ ])
This method is used to concatenate the elements of an object array with the help of a separator between each element of the object array.
Syntax:
public static string Join(string separator, params obj[] array)
- Parameters: This method takes two parameters, One of them is the separator of the type System.String which is used to define a separator according to the user’s choice. Another parameter is an array of type System.Object[] which contains the elements to be concatenated.
- Return Type: The return type of this method is System.String.
- Exception: This method can give ArgumentNullException if the array is null.
Example: In the below code, first an array of objects is created and then it is passed to the join method along with the separator to be used, here ‘, ‘ comma separator is used and after method’s return, the string is taken as the output.
using System;
namespace ConsoleApplication1 {
class Geeks {
static void Main( string [] args)
{
object [] array = { "Hello" , "Geeks" , 12345, 786};
string s1 = string .Join( ", " , array);
Console.WriteLine( "Value of string s1 is " + s1);
}
}
}
|
Output:
Value of string s1 is Hello, Geeks, 12345, 786
String.Join(String, string [ ])
This method is used to concatenate the elements of a String array with the help of a user-specified separator between each element of the string array.
Syntax:
public static string Join(string separator, params string[ ] array)
- Parameters: This method takes two parameters, One of them is the separator of the type System.String which is used to define a separator according to the user’s choice. Another parameter is an array of type System.String[] which contains the elements to be concatenated.
- Return Type: The return type of this method is System.String.
- Exception: This method can give ArgumentNullException if the array is null.
Example:In below code, first an array of strings is created and is passed to the join method along with the separator to be used, here ‘/’ slash separator is used and after method’s return, the string is print as the output.
using System;
namespace ConsoleApplication2 {
class Geeks {
static void Main( string [] args)
{
string [] array = { "hello" , " World " ,
"Geeks" , " are " , " here " };
string s1 = string .Join( "/" , array);
Console.WriteLine( "Value of string s1 is " + s1);
}
}
}
|
Output:
Value of string s1 is hello/ World /Geeks/ are / here
String.Join(String, string [ ], int pos1, int pos2)
This method is used to concatenate the elements of a String array between the specified positions with the help of a user-defined separator between each element of the array.
Syntax:
public static string Join(string separator, params string[] array, int pos1, int pos2)
- Parameters: This method takes four parameters, first one is the separator of the type System.String which is used to define a separator according to the user’s choice. Second parameter is an array of type System.String[] which contains the elements to be concatenated. Third parameter is pos1 of type System.Int32 which is the first element in array to be used and important point to remember about this parameter is that it used zero-based index. Fourth parameter is pos2 of type System.Int32 which is used to specify the number of elements of array to be used and it always starts from 1 i.e it counts the pos1’value as its first element.
- Return Type: The return type of this method is System.String. Also it can return of type String.Empty if value of pos2 is zero.
- Exceptions: This method can give ArgumentNullException if the array is null. ArgumentOutOfRangeException can be given if the pos1 or pos2 is less than zero or pos1 + pos2 is greater than the number of elements in array and also this method can give OutOfMemoryException.
Example: In below code, an array of strings is created and suppose that user wants to join the strings from position index 2 and want to cover five elements.
using System;
namespace ConsoleApplication3 {
class Geeks {
static void Main( string [] args)
{
string [] array = { "lazy" , "dog" , "jumps" , "Over" ,
"the" , "Lazy" , "fox" };
string s1 = string .Join( "-" , array, 2, 5);
Console.WriteLine( "Value of string is " + s1);
}
}
}
|
Output:
Value of string is jumps-Over-the-Lazy-fox
References:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Dec, 2022
Like Article
Save Article