Monday, March 05, 2007

The sample was compiled using the latest version of D, 1.007 at the time of writing. You can get the latest version of the D compiler here.

Problem

You want to break up a string into its individual tokens or words 

Solution

Use the split() function from std.string, like so:

import std.stdio;
import std.string;

void main(char[][] args)  {
     char[]   record     = "01 Barry Carr 17/04/1964 barry@notarealdomain.com";
     char[][] attributes = split(record);

     foreach( char[] attribute; attributes )
        writefln( attribute );
 }

There is on overloaded version of split() that allows you specify what characters are to be used as delimiters.

The foreach statement at the end of the code above is there so that the tokens can be displayed on the console.

Tags: ,