• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

LastWordTyped Function

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.
0
blue220
Asked:
blue220
  • 2
1 Solution
 
ZhaawZSoftware DeveloperCommented:
Where do you type the word? Is it in your application? If yes, is it in some single control or in a whole app?
0
 
blue220Author Commented:
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.
0
 
mikelittlewoodCommented:
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;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  iLoop: Integer;
  Result, sTemp: String;
begin
  sTemp:= '';
  Result:= '';
  // loop down from last cursor
  for iLoop := i downto 1 do
  begin
    // 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
    else
      // make sure not checking first space in a string
    if (Trim(sTemp) = '')  and (iLoop <> i) then
      // break out of the loop
      Break;
  end;
  Trim(Result);
  ShowMessage('Last word typed was "' + Result + '"');
end;

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

end.
0
 
blue220Author Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now