?
Solved

Split and then pair up a sentance

Posted on 2006-05-19
8
Medium Priority
?
135 Views
Last Modified: 2010-04-05
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!
0
Comment
Question by:wildzero
  • 5
  • 3
8 Comments
 
LVL 10

Author Comment

by:wildzero
ID: 16722775
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?
0
 
LVL 10

Author Comment

by:wildzero
ID: 16722812
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;
0
 
LVL 10

Author Comment

by:wildzero
ID: 16729918
Anyone have any other suggestions on how I could improve the above function?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16730689
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
0
 
LVL 10

Author Comment

by:wildzero
ID: 16730708
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
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 1000 total points
ID: 16730874
drop a TEdit, TButton and TMemo on a form, double click the Button1, and paste this code

procedure TForm1.Button1Click(Sender: TObject);
const
  MaxNumberOfWordGroups = 3; // "my"   "my name"   "my name is"
var
  sl: TStringList;
  slresults: TStringList;
  wordgroupcount, wordindex, joinloop: integer;
  s: string;
begin
  sl := TStringList.create;
  slresults := TStringList.Create;
  try
// Firstly lets break the text up into individual words
    sl.Text := StringReplace(Edit1.Text, ' ', #13#10, [rfReplaceAll]);
// now, we will loop through for 1  word, right up to MaxNumberOfWordGroups
    for wordgroupcount := 1 to MaxNumberOfWordGroups do
    begin
// start looking from the first word, up to the last (making allowances for the # of words we want)
      for wordindex := 0 to (sl.count - wordgroupcount) do
      begin
        s := '';
        for joinloop := wordindex to wordindex + (wordgroupcount-1) do
          s := s + ' ' + sl[joinloop];
        slresults.add(s);
      end;
    end;
// Show the results
    memo1.lines.assign(slresults);
  finally
    sl.clear;
    sl.free;
    slresults.clear;
    slresults.free;
  end;
end;

you can change the  
MaxNumberOfWordGroups = 3;
line in case you wish to show more than 3 words in a row
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16730889
you almost had it right actually :-)
0
 
LVL 10

Author Comment

by:wildzero
ID: 16737931
Thanks =)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

757 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