The Wayback Machine - https://web.archive.org/web/20240828152144/https://www.geeksforgeeks.org/c-sharp-join-method-set-1/
Open In App

C# | Join() Method | Set – 1

Last Updated : 01 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.




// C# program to demonstrate the
// Join(String, Obj [ ]) method
using System;
namespace ConsoleApplication1 {
  
class Geeks {
      
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating an object array
        // Here, It is consist of four 
        // elements only
        object[] array = {"Hello", "Geeks", 12345, 786};
  
        // Using Join method
        // Here separator is ', '( comma )
        string s1 = string.Join(", ", array);
  
        // Finally after joining process gets over
        // Getting the output of value of string s1
        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.




// C# program to demonstrate the
// Join(String, String [])
using System;
namespace ConsoleApplication2 {
  
class Geeks {
      
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a string array
        // Here It contains five 
        // elements only
        string[] array = {"hello", " World ", 
                "Geeks", " are ", "  here " };
  
        // Using Join method
        // Here separator used is '/'( slash )
        string s1 = string.Join("/", array);
  
        // Finally after join method
        // Getting the output of value of string s1
        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.




// C# program to demonstrate the
// Join(String, string [ ], int 
// pos1, int pos2) method
using System;
namespace ConsoleApplication3 {
  
class Geeks {
      
    // Main Method
    static void Main(string[] args)
    {
  
        // Creating a string array
        string[] array = {"lazy", "dog", "jumps", "Over",
                                  "the", "Lazy", "fox" };
  
        // Using Join method
        // Here separator used is '-'( hiphen )
        // from index 2 and covers upto 5 
        // elements from index 2
        string s1 = string.Join("-", array, 2, 5);
  
        // Finally after joining process gets over
        // Getting the output of value of string s1
        Console.WriteLine("Value of string is " + s1);
          
    }
}
}


Output:

Value of string is jumps-Over-the-Lazy-fox

References:



Similar Reads

LINQ | Join (Inner Join)
In LINQ, Join operators are used for integrating two data source into one data source which shares some common attributes. For example, in a bank, the manager has two lists, the first list contains the personal details and another list contains the home loan details. Now the manager wants to create a list which contains the names of those peoples w
5 min read
C# | Join Method | Set - 2
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. For the first three methods, please refer to Join() Method in C# Set -1. String.J
4 min read
Cross Join in LINQ
In LINQ, the cross join is a process in which the elements of two sequences are combined with each other means the element of first sequence or collection is combined with the elements of another sequence or collection without any key selection or any filtering condition and the number of elements present in the resulting sequence is equal to the p
5 min read
Array.GetValue() Method in C# with Examples | Set - 1
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
4 min read
C# | Graphics.DrawLine() Method | Set - 1
Graphics.DrawLine() Method is used to draw a line connecting the two points specified by the coordinate pairs. There are 4 methods in the overload list of this method as follows: DrawLine(Pen, PointF, PointF) Method DrawLine(Pen, Int32, Int32, Int32, Int32) Method DrawLine(Pen, Single, Single, Single, Single) Method DrawLine(Pen, Point, Point) Meth
4 min read
C# | String.IndexOf( ) Method | Set - 1
In C#, IndexOf() method is a string method. This method is used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found. This method can be overloaded by passing different parameters to it. String.IndexOf(char x)S
7 min read
C# | Math.Round() Method | Set - 1
In C#, Math.Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method can be overloaded by changing the number and type of the arguments passed. There are total 8 methods in the overload list of the Math.Round() method. Here we will discuss only 4 methods and r
6 min read
C# | Math.Round() Method | Set – 2
In C#, Math.Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method has another overload with which, you can specify the number of digits beyond the decimal point in the returned value. It returns the nearest value of the number with the precision equal to th
8 min read
C# | Math.Abs() Method | Set - 1
In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. Math.Abs(Decimal) Math.Abs(Double) Math.Abs(Int16) Math.Abs(Int32) Math.Abs(Int64) Math.Abs(SByte) Math.Abs(Single) Math.Abs(Decimal) This method is used to return the a
4 min read
C# | Math.Abs() Method | Set - 2
C# | Math.Abs() Method | Set – 1 In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. There are total 7 methods in its overload list. Math.Abs(Decimal) Math.Abs(Double) Math.Abs(Int16) Math.Abs(Int32) Math.Abs(Int64) Math.
3 min read
How to use Array.BinarySearch() Method in C# | Set -2
Array.BinarySearch() method is used to search a value in a sorted one dimensional array. The binary search algorithm is used by this method. This algorithm searches a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of
12 min read
C# | Byte.ToString Method | Set - 1
This method is used to convert the value of the current Byte object to its equivalent string representation. There are total 4 methods in the overload list of Byte.ToString() Method as follows: ToString(IFormatProvider) ToString(String, IFormatProvider) ToString() ToString(String) ToString(IFormatProvider) This method is used to Converts the numeri
4 min read
List FindLastIndex() Method in C# | Set -1
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) Method FindLastIndex(Int32, Predicate<T>) Met
3 min read
C# | Convert.ToBase64CharArray() Method | Set-1
This method is used to convert a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. Parameters specify the subsets as offsets in the input and output arrays, and the number of elements in the input array to convert. Syntax: public static int ToBase64CharArray (byte[] inArray,
5 min read
C# | Convert.ToBase64String() Method | Set-1
Convert.ToBase64String() Method is used to convert the value of an array of 8-bit unsigned integers to its equivalent string representation which is encoded with base-64 digits. There are 4 methods in the overload of this method which are as follows: ToBase64String(Byte[], Int32, Int32) Method ToBase64String(Byte[], Int32, Int32, Base64FormattingOp
4 min read
List FindLastIndex() Method in C# | Set -2
This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method: FindLastIndex(Predicate<T>) Method FindLastIndex(Int32, Predicate<T>) Met
5 min read
How to sort a list in C# | List.Sort() Method Set -1
List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of this method as follows: Sort(IComparer<T>) Sor
5 min read
DateTime.GetDateTimeFormats() Method in C# | Set - 1
This method is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers. There are total 4 methods in the overload list of this method: GetDateTimeFormats() GetDateTimeFormats(Char) GetDateTimeFormats(IFormatProvider) GetDateTimeFormats(Char, IFormatProvider) GetDateTimeF
4 min read
DateTime.ToString() Method in C# | Set – 1
This method is used to Converts the value of the current DateTime object to its equivalent string representation. There are total 4 methods in the overload list of this method: ToString(String, IFormatProvider)ToString(String)ToString(IFormatProvider)ToString() Here, we will discuss only first two methods. ToString(String, IFormatProvider) This met
8 min read
How to sort a list in C# | List.Sort() Method Set -2
List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of this method as follows: Sort(IComparer<T>) Sor
5 min read
How to sort an Array in C# | Array.Sort() Method Set - 1
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows: Sort<T>(T[]) Method Sort<T>(T[], IComparer<T>) Method Sort<T>(T[], Int32, Int32) Method Sort<T>(T[], Comparison<T>) Method Sort(Array, Int32, Int32, IComparer) Method Sort(Ar
7 min read
How to sort an Array in C# | Array.Sort() Method Set – 2
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method Sort(Array, Array, Int32, Int32) Method Sort(Arra
9 min read
Array.LastIndexOf Method in C# | Set - 1
Array.LastIndexOf method is used to find the last matching element in a one-dimensional array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexOf(Array, Object) LastIndexOf(Array, Object, Int32)
8 min read
Array.LastIndexOf Method in C# | Set – 2
Array.LastIndexOf method is used to find the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array. It starts the search from the last element of an array. It returns the index of the element that contains the specified value. There are 6 methods in the overload list of this method as follows: LastIndexOf(A
4 min read
How to sort an Array in C# | Array.Sort() Method Set – 3
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, IComparer) Method Sort(Array, Array, IComparer) Method Sort(Array, Array) Method Sort(Array, IComparer) Method This method sorts the elements in a one-dimensional ar
6 min read
How to sort an Array in C# | Array.Sort() Method | Set – 4
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array) Method This method sorts the elements in an entire one-dimensional array using the IComparable implementation of each element of the Array. Syntax: public static voi
5 min read
How to sort an Array in C# | Array.Sort() Method | Set – 5
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) Method This method sorts a pair of array objects based on the keys in the first array using the specified IComp
7 min read
Decimal.Round() Method in C# | Set - 1
Decimal.Round Method is used to round a value to the nearest integer or a specified number of decimal places. There are 4 methods in the overload list of this method as follows: Round(Decimal) Method Round(Decimal, Int32) Method Round(Decimal, MidpointRounding) Method Round(Decimal, Int32, MidpointRounding) Method Here, we will discuss the first tw
3 min read
C# | Int16.ToString Method | Set - 1
Int16.ToString Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows: ToString(IFormatProvider) Method ToString(String, IFormatProvider) Method ToString() Method ToString(String) Method Here we will discuss the first two methods
2 min read
C# | Int32.ToString Method | Set - 1
Int32.ToString Method is used to converts the numeric value of the current Int32 instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows:Here, we will discuss the first two methods. ToString(IFormatProvider) Method This method is used to convert the numeric value of the current instance t
2 min read
Article Tags :