How do I use the Tango LineIterator.scan()?

Alexander Pánek alexander.panek at brainsware.org
Tue Oct 14 07:18:31 PDT 2008


Jarrett Billingsley wrote:
> On Mon, Oct 13, 2008 at 10:28 PM, Alexander Pánek
> <alexander.panek at brainsware.org> wrote:
>> Jarrett Billingsley wrote:
>>> import tango.text.Util;
>>> ...
>>> // using line as the line of text.
>>> auto firstSpace = line.locate(' '); // syntactic sugar for locate(line, '
>>> ')
>>> auto secondSpace = line.locate(' ', firstSpace + 1);
>>> auto firstCol = line[0 .. firstSpace];
>>> auto secondCol = line[firstSpace + 1 .. secondSpace];
>>> auto thirdCol = line[secondSpace + 1 .. $]; // $ means line.length
>>>
>>> // now you can convert the columns as you please
>> auto columns = line.split(" ");
>> auto firstCol = columns[0];
>> auto secondCol = columns[1];
>> // ... that's a tad shorter. :P
> 
> The third column can have spaces.  This will split on them too.  Not
> to mention line.split() will allocate a new array every time it's
> called ;)

Nittypickybulubulub! It is shorter in code, qed. ;P


More information about the Digitalmars-d-learn mailing list