Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Parsing the String or Text

Author: admin | Category: Tips and Tricks

//This is how we can parse the string or text just like explode function in PHP
//use button 1 to run the 'Parsing function'
//Parsing function has 3 parameters (Char, Str: string; Count: Integer).
//Char parameter used to define the parsing delimiter, Str parameter used to define the text to be parsed
//Count parameter used to define the number of start parsing
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function Parsing(Char, Str: string; Count: Integer): string;
var
  i: Integer;
  strResult: string;
begin
  if Str[Length(Str)] <> Char then
    Str := Str + Char;
  for i := 1 to Count do
  begin
    strResult := Copy(Str, 0, Pos(Char, Str) - 1);
    Str := Copy(Str, Pos(Char, Str) + 1, Length(Str));
  end;
  Result := strResult;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  StringToParse: String;
begin
   StringToParse :=  'Abcd,efgh,ijkl,mnopq,rstu,vwxyz';
   ShowMessage(Parsing(',', StringToParse, 3));
end;
 
end.

Tags: