String.split Method
To separate substrings in the string array, we often use delimiters such as, comma, semi colon, space, slash etc. We use split method to separate strings from these types of delimiters like:
String.Join Method
String.Join method is used to join more than one string in a single string. It takes a string array and uses a separator string which is to be placed in between every element of the string array.
Syntax
String.Join(".", Strings[]);
The following sample code will show the splitting and joining of string. Here we have a string "If He Works Hard, he will succeed". The string.Split () will separate string from spaces, comma and dot and this output will be shown “If He Work Hard he will succeed”. The string.Join() will place a hyphen between every element of the string and the following output will be shown “If-He-Works-Hard- - he-will-succeed”.
Code
using System;
using System.Text;
public static void Main()
{
string mystring = "If He Works Hard, he will succeed.";
string[] s = mystring.Split(' ',',','.');
foreach (string space in s)
Console.WriteLine(space);
string whole = String.Join(" - ", s);
Console.WriteLine("Result of join: "+ whole);
Console.ReadLine();
}