Henry Olive 5 Posted December 8, 2021 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
PeterBelow 238 Posted December 8, 2021 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
Alexander Sviridenkov 357 Posted December 8, 2021 In recent Delphi there is s.SpltString() which returns array of string. Share this post Link to post
Alexander Elagin 143 Posted December 8, 2021 There is also a function SplitString() in StrUtils.pas. Share this post Link to post
Henry Olive 5 Posted December 8, 2021 Thank you so much Peter, Sviridenkov, Elagin Share this post Link to post