Home > Delphi, Uncategorized > Delphi Split String Utility

Delphi Split String Utility


There is no built-in Split function in Delphi, sometimes we need an easy way to perform string splitting just like PHP or Python does: “Hello World”.split(‘ ‘) would produce dynamic array [‘Hello’, ‘World’].

In Delphi you can perform that in different way, for example using TStringList separated text behavior but you need maintenance the TStrings instance each time performing splitting.

var
  Temp: TStrings;
begin
  Temp := TStringList.Create;
  try
    Temp.Text := StringReplace('Hello World', #32, #13#10, [rfReplaceAll]);
    // Process the list here
    // Temp[0] = 'Hello'
    // Temp[1] = 'World'
  finally
    Temp.Free;
  end;
end;

Compare in Python way:

list = 'Hello World'.split(' ')
# list[0] == 'Hello'
# list[1] == 'World'

Pretty short and comfort is it.
I try makes something easy in Delphi way, like this:

var
  List: IStrings;
begin
  List := SplitEx('Hello World', ' ');
  // List[0] = 'Hello'
  // List[1] = 'World'

Or more complex:

  with SplitEx('08:00-13:00, '-') do
  begin
    // Index 0: 08:00
    with SplitEx(Strings[0], ':') do
      // Index 0: 08
      // Index 1: 00
      dtStartTime.Time := EncodeTime(AsInt[0], AsInt[1], 0, 0);

    // Index 1: 13:00
    with SplitEx(Strings[1], ':') do
      // Index 0: 13
      // Index 1: 00
      dtEndTime.Time := EncodeTime(AsInt[0], AsInt[1], 0, 0);
end;

Strings[n] is IStrings default getter & setter for list property
AsInt also IStrings interface method that convert string value at index to Integer.

Pretty cool! Create new unit and paste this codes 🙂

type
  IStrings = interface
  ['{041D1A57-D1DE-4329-A0B0-17DC815B4449}']
    function GetObj: TStrings;
    function GetCount: Integer;
    function GetString(Index: Integer): string;
    function GetAsInt(Index: Integer): Integer;
    procedure SetString(Index: Integer; const Value: string);
    procedure SetAsInt(Index: Integer; const Value: Integer);

    property Obj: TStrings read GetObj;
    property Count: Integer read GetCount;
    property Strings[Index: Integer]: string read GetString write SetString; default;
    property AsInt[Index: Integer]: Integer read GetAsInt write SetAsInt;
  end;

  function SplitEx(const Text, Separator: string): IStrings;

implementation

type
  TStringsImpl = class(TInterfacedObject, IStrings)
  private
    FStrings: TStringList;
  protected
    function GetObj: TStrings;
    function GetCount: Integer;
    function GetString(Index: Integer): string;
    function GetAsInt(Index: Integer): Integer;
    procedure SetString(Index: Integer; const Value: string);
    procedure SetAsInt(Index: Integer; const Value: Integer);
  public
    constructor Create(const Text: string; const SplitDelimiter: string = '');
    destructor Destroy; override;
  end;

{ TStringsImpl }

constructor TStringsImpl.Create(const Text: string; const SplitDelimiter: string);
begin
  FStrings := TStringList.Create;
  if SplitDelimiter <> '' then
    FStrings.Text := StringReplace(Text, SplitDelimiter, #13#10, [rfReplaceAll])
  else
    FStrings.Text := Text;
end;

destructor TStringsImpl.Destroy;
begin
  FStrings.Free;
  inherited;
end;

function TStringsImpl.GetAsInt(Index: Integer): Integer;
begin
  Result := StrToIntDef(FStrings[Index], 0);
end;

function TStringsImpl.GetCount: Integer;
begin
  Result := FStrings.Count;
end;

function TStringsImpl.GetObj: TStrings;
begin
  Result := FStrings;
end;

function TStringsImpl.GetString(Index: Integer): string;
begin
  Result := FStrings[Index];
end;

procedure TStringsImpl.SetAsInt(Index: Integer; const Value: Integer);
begin
  FStrings[Index] := IntToStr(Value);
end;

procedure TStringsImpl.SetString(Index: Integer; const Value: string);
begin
  FStrings[Index] := Value;
end;

{ Helper }

function SplitEx(const Text, Separator: string): IStrings;
begin
  Result := TStringsImpl.Create(Text, Separator) as IStrings;
end;
Advertisements
Categories: Delphi, Uncategorized Tags: , ,
  1. Tigor Manurung
    April 27, 2012 at 11:36 pm

    keren..suhu memang top! 🙂

  2. herbalmall
    April 28, 2012 at 9:50 am

    semangat delphi nya masih ada ternyata 😀

  3. han
    August 14, 2012 at 9:13 pm

    Ini yg ane cari, sip gan

  4. July 19, 2013 at 10:37 am

    hi,
    I have not been able to give a comment, but I will listen first. thx’s

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: