LastWordTyped Function

Posted on 2005-05-13
Last Modified: 2010-05-18
Anyone know a function that will return the last word that is typed in a string regardless of where at in the string it's typed.

Example #1

String: "The boy walked to the store"
This should return the word store NOT because it's the last word in the sentence but because it's the last word that I typed.)


Example #2

String: "The boy walked to the store"
Then I change the word "boy" to "girl" in the sentence above so it looks like this.

"The girl walked to the store"

(This should return the word girl since it was the last word that was typed even though it's not the last word in the sentence)


I hope that makes sence and thanks for your replies in advance.
Question by:blue220
    LVL 11

    Expert Comment

    Where do you type the word? Is it in your application? If yes, is it in some single control or in a whole app?

    Author Comment

    being a function I would expect it to work globally (atleast where you would expect type strings ie. in memos , edits , and richedits...) and yes it is in my app.
    LVL 15

    Accepted Solution

    As long as you are not cut and pasting, maybe this idea would work.

    Also, what event are you triggering that asks your application what the last word typed was?

    unit Unit1;


      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;

      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Memo1KeyUp(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        i: Integer;
        { Public declarations }

      Form1: TForm1;


    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
      iLoop: Integer;
      Result, sTemp: String;
      sTemp:= '';
      Result:= '';
      // loop down from last cursor
      for iLoop := i downto 1 do
        // go backwards and get each letter
        sTemp:= copy(Memo1.Lines.Text, iLoop, 1);
        // check not empty, i.e. a space
        if (Trim(sTemp) <> '') then
          // add character
          Result:= sTemp + Result
          // make sure not checking first space in a string
        if (Trim(sTemp) = '')  and (iLoop <> i) then
          // break out of the loop
      ShowMessage('Last word typed was "' + Result + '"');

    procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
      // store current position of cursor in memo
      i := Memo1.SelStart;


    Author Comment

    Thx for the reply I have been busy working on this project and although your code will not really work for me  this time I am sure I will find a use for it down the road so I will just award you the points for your effort.

    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

    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…
    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…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    733 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

    19 Experts available now in Live!

    Get 1:1 Help Now