Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

Split and then pair up a sentance

Hi there

Lets say I have a sentance like this

"My name is bob"

what I would like is a way to be able to get a list of the words like

my
name
is bob

and then get a list of all the words like

my name
name is
is bob

then again like

my name is
name is bob

Anyone have any suggestions?
=) Thanks guys!
Avatar of wildzero
wildzero

ASKER

This function can be used to split the sentance
procedure Split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
  Assert(Assigned(Strings));
  Strings.Clear;
  Strings.Delimiter := Delimiter;
  Strings.DelimitedText := Input;
end;

used like


SL := TStringlist.create;
Split(' ', 'My name is bob', SL);
Now SL contains all the words line by line
how to tackle the next part?
Check this out
Uses a Button, Listbox and a SPinEdit

var
  SL : TStringlist;
  i,b : integer;
  sWords : String;
begin
  SL := TStringlist.create;
  Split(' ', Edit1.text, SL);

  Listbox1.items.assign(SL);

  For i := 0 to SL.count-1 do
    Begin
      sWords := '';
      If (i + SpinEdit1.value) <= SL.Count then
        begin
          For b := 0 to SpinEdit1.Value-1 do
            begin
              If sWords = '' then
                sWords := SL[i+b]
              else
              sWords := sWords + ' '+ SL[i+b];
            end;

        end;
      ListBox1.Items.add(sWords);
    End;
Anyone have any other suggestions on how I could improve the above function?
Avatar of TheRealLoki
you say "words" but then use
my
name
"is bob"

should this be
my
name
is
bob
?
if so, I'll write something to do what you ask
oh yea whops :P hehe
yea that should be right - didn't hit the enter key

See if you can improve on my code there
ASKER CERTIFIED SOLUTION
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you almost had it right actually :-)
Thanks =)