[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4071
  • Last Modified:

How parse a delphi delimited string

I need a routine to parse a delimited string.  This like the php explode function.  I found the function below and it looks like it will work with my string,  but don't know how make the call and get the individual parsed string elements.

For example, the string:
my string is defined as follows:
OCRString : String;
and contains the following:

19344|643994|02032009|02132009|8632253|65.18|0.00|0.00|1337.06

Could someone please show me how to make the function call

 See code below.
function explode(const separator, s: string; limit: Integer = 0): TDynStringArray;
var SepLen: Integer;
    F, P: PChar;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = '' then
    begin
      SetLength(Result, 1);
      Result[0] := S;
      Exit;
    end;
  SepLen := Length(Separator);
 
From: http://www.delphi3000.com/articles/article_3609.asp  (see comments on delphi3000 page)
 
 unction Explode(const Separator, S: String; Limit: Integer = 0): TDynStringArray;
  var
    SepLen: Integer;
    F, P: PChar;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = '' then
    begin
      SetLength(Result, 1);
      Result[0] := S;
      Exit;
    end;
  SepLen := Length(Separator);
 
  P := PChar(S);
  while P^ <> #0 do
    begin
      F := P;
      P := AnsiStrPos(P, PChar(Separator));
      if (P = nil) or ((Limit > 0) and (Length(Result) = Limit - 1)) then
        P := StrEnd(F);
      SetLength(Result, Length(Result) + 1);
      SetString(Result[High(Result)], F, P - F);
      F := P;
      //ERROR in case if string is finished with separator
      if P = Separator then
        SetLength(Result, Length(Result) + 1);
      //END of modification
      while (P^ <> #0) and (P - F < SepLen) do
        Inc(P);
    end;
end;

Open in new window

0
IT79637
Asked:
IT79637
  • 2
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
List := TStringList.Create;
try
  List.Delimiter := Separator;
  List.DelimitedText := s;
 
  for I := 0 to List.Count-1 do
    ShowMessage(List[I]); // like this you have access to the elements

finally
  List.Free;
end;
0
 
IT79637Author Commented:
Works perfectly.  
Thanks much!!!
0
 
MerijnBSr. Software EngineerCommented:
If you have a recent Delphi version, you want to add StringDelimiter to that:
List := TStringList.Create;
try
  List.StrictDelimiter := true;
  List.Delimiter := Separator;
  List.DelimitedText := s;
 
  for I := 0 to List.Count-1 do
    ShowMessage(List[I]); // like this you have access to the elements
 
finally
  List.Free;
end;

Open in new window

0
 
Geert GruwezOracle dbaCommented:
so that solves the dratted "space" issue ?
0
 
MerijnBSr. Software EngineerCommented:
> so that solves the dratted "space" issue ?

Yes! :D (In other words, it finally works the way you want to in this situation).

From the help:

Use this property to specify whether the Delimiter is the only value used within the DelimitedText property. If set to True, individual strings in DelimitedText are separated only by the character that is the value of Delimiter. If set to False, individual strings in DelimitedText can be separated by a space, a non-printable character, or the character that is the value of Delimiter.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now