Framework Tips VII: Splitting strings

Joe Devel­oper once had to read options from con­fig­u­ra­tion file, where each line looked basi­cally like this:

option:   first;second;third;;fifth;

Each line con­sisted of option name, fol­lowed by few spaces, and its para­me­ters delim­ited by semi­colons. Now the ques­tion is: how would Joe get to those para­me­ters? The most ele­gant way would be using Reg­u­lar Expres­sions, but Joe has strong allergy to them. For this sim­ple exam­ple String.Split method will do.

Joe rolled up his sleeves and crafted that beau­ti­ful mas­ter­piece of code:

string options = @"option:   first;second;third;;fifth;";

string[] strings = options.Split(';');

foreach (string s in strings)

    Console.WriteLine(s);

    

Console.WriteLine("That's all. Press any key to exit.");

Console.ReadKey(false);

How­ever, that didn't work quite as well as expected:

string.split.ss0

Joe suc­cess­fully split each option to dif­fer­ent line, but he had also some empty lines, as well as addi­tional stuff along with first option. For­tu­nately Joe noticed that thanks to params key­word, he can pass more para­me­ters to Split method. He quickly altered his code, by adding two more split char­ac­ters, and chang­ing fore­ach loop to for, to leave option ele­ment out.

string options = @"option:   first;second;third;;fifth;";

string[] strings = options.Split(';', ':', ' ');

for (int i = 1; i < strings.Length; i++)

    Console.WriteLine(strings[i]);

 

Console.WriteLine("That's all. Press any key to exit.");

Console.ReadKey(false);

string.split.ss1 Proud of him­self Joe ran his pro­gram to notice, that the out­put, while bet­ter, still didn't look as he needed. While he man­aged to get each para­me­ter into its own line, he still got those annoy­ing empty lines.

His imme­di­ate idea was to add a con­di­tion, to check wether given string is empty before writ­ing it out, but then he remem­bered the quote:

When debug­ging, novices insert cor­rec­tive code; experts remove defec­tive code.  ~Richard Pattis

And since Joe was a true expert, he decided to remove the defect. He looked here and there, and dis­cov­ered, that StringSpli­tOp­tions enum, was exactly what he needed. For­tu­nately, there was even an over­load of Split method that accepted StringSpli­tOp­tions as one of its argu­ments. Joe quickly altered 2nd line of  his code to:

string[] strings = options.Split(new char[] {';', ':', ' '}, StringSplitOptions.RemoveEmptyEntries);

string.split.ss2 And he finally was happy with what he saw when he ran the pro­gram. His hap­pi­ness didn't last quite long, how­ever. Only 'till he met his boss, to be exact, who told him, that only first two para­me­ters are impor­tant, and all remain­ing should be left out.

I can do it! — Joe thought. — All I need to do is to limit the for loop to stop after two first ele­ments. Then he thought — Why waste resources and time to split fur­ther, when all I need are just two first para­me­ters. — He recalled the quote once again, and decided, that as true expert he would find another solution.

Then he found yet another over­load, of Split method, that accepted addi­tional para­me­ter: inte­ger, spec­i­fy­ing max­i­mum num­ber of sub­strings to be pro­duced. Joe quickly adjusted his code to the new requirements:

string options = @"option:   first;second;third;;fifth;";

string[] strings = options.Split(new char[] {';', ':', ' '}, 4, StringSplitOptions.RemoveEmptyEntries);

for (int i = 1; i < strings.Length && i < 3; i++)

    Console.WriteLine(strings[i]);

 

Console.WriteLine("That's all. Press any key to exit.");

Console.ReadKey(false);

Now he was really proud of him­self — with one sim­ple method, he was able to accom­plish the task.

Tech­no­rati Tags: ,
  • http://renxe.wordpress.com/ RenXe

    nice tips :D thankyou