- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Join() Method
The Join() method in C# is used to concatenate all the elements of a string array, using the specified separator between each element.
Syntax
The syntax is as follows -
public static string Join (string separator, string[] val);
Above, separator is the separator that gets included in the string. The val parameter is the array containing elements to concatenate.
Example
Let us now see an example -
using System; public class Demo { public static void Main(string[] args) { string[] strArr = {"One", "Two", "Three", "Four" }; Console.WriteLine("String Array..."); foreach(string s in strArr) { Console.WriteLine(s); } string str = string.Join("/", strArr); Console.WriteLine("Result (after joining) = " + str); } }
Output
This will produce the following output -
String Array... One Two Three Four Result (after joining) = One/Two/Three/Four
Example
Let us now see another example -
using System; public class Demo { public static void Main(string[] args) { string[] strArr = {"AB", "BC", "CD", "DE", "EF", "FG", "GH", "IJ" }; Console.WriteLine("String Array..."); foreach(string s in strArr) { Console.WriteLine(s); } string str = string.Join("*", strArr); Console.WriteLine("Result (after joining) = " + str); } }
Output
This will produce the following output -
String Array... AB BC CD DE EF FG GH IJ Result (after joining) = AB*BC*CD*DE*EF*FG*GH*IJ
Advertisements