Jump to content

Recommended Posts

I wish everyone a healthy day.

 

  S:= Edit1.Text;  // which is  A1 A2 A3 A4

  with below code i get only  A1 but i need to get A1 A2 (First 2 words )
  X:=Pos(' ',S);
  Left2Words:= Copy(S,1,X-1);
  Edit2.Text := Left2Words;

 

Thank You

 

 

 

 

Share this post


Link to post
7 minutes ago, Henry Olive said:

I wish everyone a healthy day.

 

  S:= Edit1.Text;  // which is  A1 A2 A3 A4

  with below code i get only  A1 but i need to get A1 A2 (First 2 words )
  X:=Pos(' ',S);
  Left2Words:= Copy(S,1,X-1);
  Edit2.Text := Left2Words;

 

Thank You

 

 

 

 

I think the best way is to dissect the input into "words" and then assemble the parts you want from that list.

 

procedure TForm1.Button1Click(Sender: TObject);
var
  LWords: TStringlist;
begin
  LWords := TStringlist.Create();
  try
    LWords.Delimiter := ' ';
    LWords.DelimitedText := edit1.Text;
    if LWords.Count >= 2 then
      edit1.Text := LWords[0] + ' ' + LWords[1];
  finally
    LWords.Free;
  end;
end;

Note that there also is a

function PosEx(const SubStr, S: string; Offset: Integer): Integer

in unit System.StrUtils that allows you to start the search at a position other than 1.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×