Find function

whether is there the equivalent function for the Visual Basic "Split" functions in Delphi?
ourAsked:
Who is Participating?
 
GoodTimesConnect With a Mentor Commented:
I wrote a function, similar to what you need, with a little bit more functionality: what it does is receive a string and a separator, and returns a dynamic array which has in each cell the separated text:

Type
   TParams : array of string;
....
....
....
function GetParams(var Params : TParams; S : String; Separator : Char) : integer;
var
  i, j, t : integer;
  tmpStr : String;
  Continue : Boolean;
begin
  j := 0;

  for i := 1 to Length(S) do
    if S[i] = Separator then Inc(j);
  Inc(j);

  SetLength(Params, j);

  Result := j;

  tmpStr := S;

  Continue := True;
  i := Length(tmpStr);
  for t := j-1 downto 0 do
  begin
    while Continue do
    begin
      if tmpStr[i] = Separator then
        Continue := false
      else Dec(i);
    end;
    Params[t] := Copy(tmpStr, i +1, Length(tmpStr) - i +1);
    Delete(tmpStr, i, Length(tmpStr) - i+1);
    i := Length(tmpStr);
    Continue := True;
  end;
end;

---------------------

here Params is the dynamic-array and the result of the function is its length. (from 0 to n-1).

Does this help?

Regards,
Omri
0
 
MotazCommented:
What did Split function did in VB ?
0
 
MotazCommented:
Delete function.

e.g.

Text:= 'Hello there';
Delete(Text, 1, 2);
ShowMessage(Text); // 'llo there'

Motaz
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
MotazCommented:
Delete(Str, Position, Length);

Str: String you want to cut from.

Position: First position of substring you want to cut.

Length: Length of substring to be deleted
0
 
MotazCommented:
Sorry our, I'm wrong Split function is not like Delete, instead use TStringList like this example:

-Drop a button and a ListBox

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStringList;
begin
  List:= TStringList.Create;
  List.CommaText:= 'First,Second,Third';
  ListBox1.Items:= List;
  List.Free;

end;

Motaz
0
 
MotazCommented:
Items must be seperated by comma (,)

0
 
ourAuthor Commented:
Sorry ! Maybe I give  an example.
In VB
Split("we:are:friend",":")
Answer is
we
are
friend
0
 
ourAuthor Commented:
TStringList's CommaText are make up "spaces, commas or quotes",but ":"?
0
 
florisbCommented:
Errr.

use this function to split a string seperated by somthing.

F.e.

line := 'a,b,c,d,e'
value1 := Leesveld(line,','); //value1 = a; line = b,c,d,e
value2 := Leesveld(line,','); //value2 = b; line = c,d,e

et cetera.

Floris.


function TForm1.LeesVeld(var regel: String; Seperator : String):String;
///
begin
  Result := Copy(regel, 0, pos(Seperator, regel)-1);
  regel := Copy(regel, pos(Seperator, regel)+1, length(regel));

/// empty fields.
if Result = '' then
   Result := '0';
end;

0
 
kretzschmarCommented:
hi our,

(do you miss a function,
then create it byself ;-))

similar to florisb

Function SplitIt(Var StrToSplit : String; SplitStr : String) : String;
begin
  Result := copy(StrToSplit,1,pos(SplitStr,StrToSplit)-1);
  if Result = '' then
  begin
    Result := StrToSplit;
    StrToSplit := '';
  end
  else  Delete(StrToSplit,1,pos(SplitStr,StrToSplit)+Length(SplitStr)-1);
end;

//usage
procedure TForm1.Button1Click(Sender: TObject);
var S : String;
begin
  S := '1,2,3,4,5,6';
  while S <> '' do Listbox1.Items.Add(SplitIt(s,','));
  S := '1xx2xx3xx4xx5xx6';
  while S <> '' do Listbox1.Items.Add(SplitIt(s,'xx'));
end;

meikl
0
 
robtanner129Commented:
Define the function, please.  What does it take as parameters, what does it return as a result?
What do you want?
Is this good?

Function Split(s: string): TStringList;
0
 
robtanner129Commented:
Probably it would be wiser to use a dynamic array more familiar to Delphi users.  In this case, a TStringList is more appropriate, although I realize this is more of a style point.  This avoids all the headaches of casting pointers etc.  

Again, a clear definition of the function has not been given.

Is this good?

Function Split(s: string; Delimiter:Char): TStringList;
0
All Courses

From novice to tech pro — start learning today.