A program may wish to process data from an Excel spreadsheet. Excel has the capability to export a worksheet in Comma Separated Value (CSV) format. Using the string Split() method allows you to extract the values from in between the commas. Similarly, the string Join() method will take individual values from an array and combine them with a separator, such as a comma. The listing below shows how to use the string Split() and Join() methods:


using System;
namespace CSharpSample
{
class StringJoinSplit
{
static void Main(string[] args)
{
// comma delimited string
string commaDelimited = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
Console.WriteLine("Original Comma Delimited String: \n{0}\n", commaDelimited);
/*
OUTPUT
Original Comma Delimited String:
Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
*/

// separate individual items between commas
string[] year = commaDelimited.Split(new char[] {','});
Console.WriteLine("Each individual item: ");
foreach(string month in year)
{
Console.Write("{0} ", month);
}
Console.WriteLine("\n");
/*
OUTPUT
Each individual item:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
*/

// combine array elements with a new separator
string colonDelimeted = String.Join(":", year);
Console.WriteLine("New Colon Delimited String: \n{0}\n", colonDelimeted);
/*
OUTPUT
New Colon Delimited String:
Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec
*/

string[] quarter = commaDelimited.Split(new Char[] {','}, 3);
Console.WriteLine("The First Three Items: ");
foreach(string month in quarter)
{
Console.Write("{0} ", month);
}
Console.WriteLine("\n");
/*
OUTPUT
The First Three Items:
Jan Feb Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
*/

string thirdQuarter = String.Join("/", year, 6, 3);
Console.WriteLine("The Third Quarter: \n{0}\n", thirdQuarter);
/*
OUTPUT
The Third Quarter:
Jul/Aug/Sep
*/
}
}
}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/zqo0wODQxpY/14173