whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

38 Visitors Online


 
...Explode / Implode a String?
Autor: Jan Christian Meyer [Hazard]
[ Print tip ]  

Tip Rating (43):  
     


// 1. ...............................................

type
  
TStrArray = array of string;

function Explode(var a: TStrArray; Border, S: string): Integer;
var
  
S2: string;
begin
  
Result  := 0;
  S2 := S + Border;
  repeat
    
SetLength(A, Length(A) + 1);
    a[Result] := Copy(S2, 0,Pos(Border, S2) - 1);
    Delete(S2, 1,Length(a[Result] + Border));
    Inc(Result);
  until S2 = '';
end;

// How to use it:
// Und hier ein Beispiel zur Verwendung:

procedure TForm1.Button1Click(Sender: TObject);
var
  
S: string;
  A: TStrArray;
  AnzTokens, i: Integer;
begin
  
S := 'Ein=Text=durch=Geleichzeichen=getrennt';
  AnzTokens := Explode(A, '=', S);
  for i := 0 to AnzTokens -1 do
    
Memo1.Lines.Add(A[i]);
end;

// 2. ...............................................

{
  * These 2 functions are from the programming language PHP, unite certainly well-known.
  * Now one can use it also in Delphi:)

  * Diese 2 Funktionen sind aus der Programmiersprache PHP, einigen bestimmt bekannt.
  * Nun kann man sie auch in Delphi verwenden :)
}

{...}

//* Needed type declaration
//* Benötigte Typendeklaration
type
  
TExplodeArray = array of String;

{...}

function Implode(const cSeparator: Stringconst cArray: TExplodeArray): String;
var
  
i: Integer;
begin
  
Result := '';
  for i := 0 to Length(cArray) -1 do begin
    
Result := Result + cSeparator + cArray[i];
  end;
  System.Delete(Result, 1, Length(cSeparator));
end;

function Explode(const cSeparator, vString: String): TExplodeArray;
var
  
i: Integer;
  S: String;
begin
  
S := vString;
  SetLength(Result, 0);
  i := 0;
  while Pos(cSeparator, S) > 0 do begin
    
SetLength(Result, Length(Result) +1);
    Result[i] := Copy(S, 1, Pos(cSeparator, S) -1);
    Inc(i);
    S := Copy(S, Pos(cSeparator, S) + Length(cSeparator), Length(S));
  end;
  SetLength(Result, Length(Result) +1);
  Result[i] := Copy(S, 1, Length(S));
end;



 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners