Whole programming languages have been designed (*cough* perl) so that we can cut delimited strings into bits and string them back together. For this purpose C# provides the String.Split() and String.Join() functions. You specify how you would like to split or merge the string and they do the work. In this post we look at some common example uses and then put together a simple CSV (comma separated values) parser.
Cutting a string using a delimiter
string Msg = “1997,Ford,E350,Super luxurious truck”;
The String.Split function allows you to specify one or more separators. If we want to cut the above string at each comma we would do the following:
string Msg = "1997,Ford,E350,Super luxurious truck"; string[] Data = Msg.Split(new char[] {','}); int Cnt = 0; foreach(string Field in Data) Console.WriteLine("{0} = {1}",Cnt++,Field);
This outputs:
0 = 1997
1 = Ford
2 = E350
3 = Super luxurious truck
The split method takes an array of char[] as its parameter. It is possible to specify more than one separator. So if you would like to split a string on both comma’s and exclamation marks you could do the following:
string Msg = "The world is not enough ! I have one, two, no three! plans for conquering it"; string[] Data = Msg.Split(new char[] {',','!'});
If you specify nothing at all then String.Split() will break on a predefined set of separators. These include spaces (all Unicode defined spaces), line breaks, tabs etc.
If you don’t want the returned array to contain empty strings, you can specify StringSplitOptions.RemoveEmptyEntries
string Msg = "One,Two,,Four,,,Six,,Eight"; string[] Data = Msg.Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries);
Putting things back together again using String.Join()
To quickly put things back together again we just String.Join(), its use is simple: You specify the character you would like to use to join, and pass an array of strings and the first element in the array to use. Lastly it is possible to limit the number of items Joined together.
String[] val = {“ape”,”monkey”,”lion”,”human”,”woman”};
Console.WriteLine(“{0}”, String.Join(“|”,val,0,4));
This results in:
ape|monkey|lion|human
Building a Comma Separated Values (CSV) reader / writer using String.Split and String.Join
Comma Seperated Values files are created by spreadsheets (such as Excel, OpenOffice Calc), databases (Microsoft Access). They are used to quickly transport data between different programs. As we can now split input, and put it back together again I have created a very simple CSV reader and writer. It can read and write a CSV file and for good measure display to contents on the console.
Note that this reader is very simple, it does not deal with many potential data formatting issues that are perfectly legal for a CSV file. If you intend to import large data sets from databases you will want to create something stronger.
To play with this code the following data set provides a little bit of history on Japanese car manufacturers. It contains the name of the company, its founding year, current number of employees and founders name.
Data.CSV
Toyota,1937,316000,Kiichiro Toyoda
Mazda,1920,39364,Jujiro Matsuda
Honda,1948,167231,Soichiro Honda
using System; using System.IO; using System.Collections; using System.Text; namespace consoleoutput { class MainClass { public static ArrayList ReadCSV(string FileName) { ArrayList DataRows = new ArrayList(); // Create a Streamreader and open the file using (StreamReader sr = new StreamReader(FileName)) { String line; // Read lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { // Skip over empty lines if (line.Length == 0) continue; // Split the line at the comma string[] DataColumn = line.Split(new char[] {','}); // Add the row of data to the ArrayList DataRows.Add(DataColumn); } } return DataRows; } public static void WriteCSV(string FileName, ArrayList CSVData) { // Create a StreamWriter and open the file using (StreamWriter sw = new StreamWriter(FileName)) { foreach(string[] CSVRow in CSVData) { string line = String.Join(",",CSVRow); sw.WriteLine(line); } } } public static void PrintCVS(ArrayList CSVData) { foreach(string[] CSVRow in CSVData) { string line = String.Join(",",CSVRow); Console.WriteLine(line); } } public static void Main(string[] args) { ArrayList myArrayList = ReadCSV("/home/martijn/cars.csv"); PrintCVS(myArrayList); WriteCSV("/home/martijn/output.csv",myArrayList); } } }
Image credit: Kacey
This is a post from Martijn's C# Coding Blog.