![]() |
VOOZH | about |
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.
Example: Basic use of the Join() method.
Geeks for Geeks
This method can be overloaded by passing different parameters to it.
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)
Example: Using the Join() method with the array of objects.
Value of string s1 is Hello, Geeks, 12345, 786
Explanation: In the above example, we first create an array of objects. Then it is passed to the join method along with the separator which is a ‘, ‘ comma, after the method returns a string as shown in the output.
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)
Example: Using the Join() method with an array of strings.
Value of string : Hello, Geeks, How, are, you
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[] arr, int start, int count)
Parameters:
Return Type:
Exception:
Example: Using the Join() method to separate the string with the specified count.
Value of string res is : Hello, Geeks
Explanation: In the above example, we pass 4 parameters one is the separate and the array and after that the starting index in which we want to start the separation from the string array and the count which defines the number count of string we want to add.
This method is used to concatenate the members of a constructed collection of type String, using the specified separator between each member.
Syntax:
public static string Join(string separator, IEnumerable L1)
Parameters:
Return Type: This method returns a string of type System.String, which consists of the members of values delimited by the separator string. If values has no members, the method returns String.Empty.
Exception: This method can give ArgumentNullException if the L1 is null.
Example: Joining the elements of a list of type String with the separator hyphen(-) using String.Join() method.
The result value is res: Hello-Geeks-How-are-you?
This method is a generic<T> overload of the String.Join() method, which is used to concatenate elements of any generic collection (that implements IEnumerable<T>) into a single string, with a specified separator between each element.
Syntax:
public static string Join(string separator, IEnumerable T1)
Parameters:
Return Type: This method returns a string of type System.String, which consists of the members of the collection delimited by the specified separator. If the collection has no elements, the method returns String.Empty.
Exception: This method can give ArgumentNullException, if the T1 is null.
Example: Using the Join() method to separate the list of user-defined data type with the seperator hyphen (-).
List of Books: The values res: C#-Java-Kotlin-Javascript
Explanation: In the above example, we create a class named Books and store the data of user-defined type (Book) and store it in the list and then separate it into a single string using the Join() method with the separator - (hyphen).