Pages

Friday, October 31, 2014

How to implode array in delphi

As in the previous post how to explode some string in delphi, we can also make the reverse, it is implode. In delphi we can make implode function as in php.

Below is the function:

//////////////////////////////////////////////////////////////////////////
// Procedure - implode
// Author    - Ronald Buster
// Remarc    - PHP like implode function
//
// Returns a string containing a string representation of all the array
// elements in the same order, with the glue string between each element.
//////////////////////////////////////////////////////////////////////////

function implode(const glue: string; const pieces: array of string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 0 to High(Pieces) do
    Result := Result + Glue + Pieces[I];
  Delete(Result, 1, Length(Glue));
end;

To use function above you can copy the sample code below :

procedure TForm1.Button1Click(Sender: TObject);
var
  ls_glue:string;
  arr_fruit:array[0..3] of string;
begin
  ls_glue := ', ';
  arr_fruit[0] := 'banana';
  arr_fruit[1] := 'mango';
  arr_fruit[2] := 'pear';
  arr_fruit[3] := 'orange';

  ShowMessage('Here are my favourite fruit: ' + implode(ls_glue, arr_fruit));
end;
Thanks to Ronald Buster for this function

Friday, September 26, 2014

How to explode some string in delphi

How to explode some string in delphi

In delphi, you can make a function explode as in php, copy the code below to do that :

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);

  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;
    while (P^ <> #0) and (P - F < SepLen) do
      Inc(P);
  end;
end;

How to use it? 

To use function above you can call it from button click as example below :
procedure TForm1.Button1Click(Sender: TObject);
var
  ls:string;
  i:integer;
  arr:TDynStringArray;
begin
  ls := 'apple;banana;mango;orange;melon';
  arr := explode(';', ls);
  for i := 0 to Length(arr)-1 do
    ShowMessage(arr[i]);
end;

Thursday, September 25, 2014

How to remove line breaks in delphi

How to remove line breaks from text in Delphi? 

You can use stringreplace function.
  • function StringReplace (const SourceString, OldPattern, NewPattern : string; Flags : TReplaceFlags) : string;
The StringReplace function replaces the first or all occurences of a substring OldPattern in SourceString with NewPattern according to Flags settings. The changed string is returned.

The Flags may be none, one, or both of these set values:
  • rfReplaceAll : Change all occurrences
  • rfIgnoreCase : Ignore case when searching
These values are specified in square brackets.

Below are the sample code :
var
   before, after : string;
begin
   before:='Some text with' + #10#13 + 'line break';
   after := StringReplace(StringReplace(before, #10, ' ', [rfReplaceAll]), #13, ' ', [rfReplaceAll]);
   ShowMessage(before);   
   ShowMessage(after);
end;


Don't Forget To Join Our Community
×
Widget