Solved

I need to create a array of words from a string

Posted on 2006-07-04
4
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now