Solved

I need to create a array of words from a string

Posted on 2006-07-04
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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