function IndexOfChar( S, ' '); r:=s-IndexOfChar; laststring:=сору(s,IndexOfChar,r) EditN.Caption:=laststring; 

Here we copy the entire line after the character (space). How to copy only the second word? Ie the word after the space.

  • Your code is wrong, where did you tear it? - SoftR
  • nakhimichil himself)) - Zein

2 answers 2

If, I understood correctly, then you need to copy only the second word from the source line, and the words are separated by spaces, here's the code:

  procedure TForm1.Button1Click (Sender: TObject);
 Var
   SS, S: String;
 begin
   SS: = Edit1.Text;

 S: = Copy (SS, Pos ('', SS) +1, Length (SS) -Pos ('', SS));
   If (Pos ('', S)> 0) Then
     S: = Copy (S, 1, Pos (", S) -1);

 Edit2.Text: = S;
 end;
  • Why do you need a P variable like Word? Why do you use outdated POS instead of the recommended AnsiPos? Fairly the impression that was torn from the Internet from an old resource, where Delphi4 is still being considered - SoftR
  • I wanted to use the variable to maintain the position of the space, but only wanted. And I always use the Pos function: I’m used to it already, but they always write about it and in the literature. And, here, about AnsiPos did not hear anything yet. - DelphiM0ZG
  • Probably not very fresh books, here is a description of your function, and a warning why you should not use it, as well as a link to AnsiPos . - SoftR
  • Well, the advantage of AnsiPos is that it works with Cyrillic and this advantage is quite enough to start using this function, but you won’t get it right away on the fly;

Here is my version of the function that returns the word you need:

  Function WordFromIndex (s:string;I:integer):String; Var sl:TStringList; begin sl:=TstringList.Create; s:=StringReplace(s,#32,#13,[rfReplaceAll, rfIgnoreCase]); sl.Clear; sl.Text:=s; Result:=sl.Strings[i-1]; sl.Free; end; 

Use this:

  WordFromIndex (s,i); 

Where S is a string is long, and I is the number of a word in a string, the result will be a word.

PS Immediately I warn you, wrote just like that, a lot is not taken into account. It is assumed that the line does not begin with a space, that there is strictly one space between words, etc. I just showed another solution to the problem.