Solved

I need to create a array of words from a string

Posted on 2006-07-04
4
172 Views
Last Modified: 2010-04-05
Hello guys

I have a string with this value:

Str := 'house bees dogs cats sea'

I would like to get all words and put it in an array, this way:

V[0]:= house
V[1]:= bees
V[2]:= dogs
V[3]:= cats
V[4]:= sea

How could I do that?
thanks
0
Comment
Question by:hidrau
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 100 total points
ID: 17038908
you can easily put them into a stringlist instead

var
    sl: tstringlist;
begin

sl := tstringlist.create;
  sl.text := stringreplace( str, ' ', #13#10, [rfReplaceAll]);


finally
  sl.free;
end;


or if you have delphi7 you can do
sl.Delimiter := ' ';
sl.DelimitedText := str;
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 100 total points
ID: 17038925
to do it with an open ended array you can do

procedure TForm1.Button1Click(Sender: TObject);
  const
    str = 'this is a test';
  var
    myarray: array of string; //open ended array
    i: integer;
    spacefound: integer;
    fullstr, partstr: string;
begin
  fullstr := trim(str);

  spacefound := pos(' ', fullstr);
  while spacefound > 0 do
  begin
    partstr := copy(fullstr, 1, spacefound-1);
    delete(fullstr, 1, spacefound);
    fullstr := trim(fullstr); // in case there are 2 spaces in a row
    setlength(myarray, length(myarray)+1);
    myarray[length(myarray)-1] := partstr; //zero based
    spacefound := pos(' ', fullstr);
  end;
  if fullstr <> '' then
  begin
    setlength(myarray, length(myarray)+1);
    myarray[length(myarray)-1] := fullstr;
  end;

// now display them

  for i := 0 to length(myarray)-1 do
  begin
    memo1.lines.add(inttostr(i) + ' - ' + myarray[i]);
  end;
end;
0
 
LVL 3

Accepted Solution

by:
Ubethatway earned 400 total points
ID: 17039902
This should do exactly ehat you want:

var list: TStringList;
    myArray: array of String;
    i: integer;

  list := TStringList.create();
  ExtractStrings([' '], [], PAnsiChar(myString), list);

  setLength(myArray, list.Count);

  for i := 0 to list.Count-1 do
    myArray[i] := list.Strings[i];

//where myString is the string you want to split
0
 
LVL 1

Author Comment

by:hidrau
ID: 17041661
Thanks guys

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 103
control image tags in a string ? 12 141
FMX and jaudiotracker playing memory stream 29 85
Wincontrol not (correctly) drawn 15 40
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question