Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

I need to create a array of words from a string

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

Technology Partners: 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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

636 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