[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need to create a array of words from a string

Posted on 2006-07-04
4
Medium Priority
?
177 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 400 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 400 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 1600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

826 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