Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

How to split a string into an array

By Zarko Gajic, About.com

A simple function that accepts a string and a delimiter char, splits a string into tokens (TStringList items) delimited with a char value.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.Button1Click(Sender: TObject) ;
var
   A: TStringList;
begin
   A := TStringList.Create;
   try
     Split(' ', 'your delphi guide', A) ;
     ShowMessage(a[0]) ; //your
     ShowMessage(a[1]) ; //delphi
     ShowMessage(a[2]) ; //guide
   finally
     A.Free;
   end;
end;

}
procedure Split
   (const Delimiter: Char;
    Input: string;
    const Strings: TStrings) ;
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to get DBGrid Cell coordinates
« Adding QuickReports to the Delphi 7 Component Palette

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2002 Delphi Tips
  7. How to split a string into an array

©2009 About.com, a part of The New York Times Company.

All rights reserved.