Lazarus

Free Pascal => Beginners => Topic started by: JLWest on March 20, 2019, 01:50:20 am

Title: ExtractWordPos ?
Post by: JLWest on March 20, 2019, 01:50:20 am

function ExtractWordPos( N: Integer;
                                     const S: string;
                                    const WordDelims: TSysCharSet;
                                   var Pos: Integer) : string;

I think I would like to use this function but not sure how to call it.

Var
 Answer : String;
 aPos      : Integer;
 Item      : String = 'Now is the time for all good men';

begin
Answer := ExtractWordPos(4,Item,['  '] , aPos);

And wouldb't it return 4 because I specifying 4 in  "N: Integer";

It's obvious that I'm not reading this right;
Title: Re: ExtractWordPos ?
Post by: lucamar on March 20, 2019, 02:11:45 am
The function returns the Nth word as result and the character position in which it was found in Pos

With your example it should return "time" and "aPos = 12". Does it return something else?
Title: Re: ExtractWordPos ?
Post by: BobDog on March 20, 2019, 02:51:13 am

According to the help file
. . .
function ExtractWordPos(
  N: Integer;
  const S: string;
  const WordDelims: TSysCharSet;
  out Pos: Integer
):string;


N   Which word to extract
S    String to extract a word from.
WordDelims  Characters to use as word delimiters
Pos    On return, contains the position of the N-th word.
. . .
But if I make the deliminator more than one character I get an error.
So, just one space as deliminator.
Code: Pascal  [Select][+][-]
  1.    
  2.  
  3. program pos;
  4. uses strutils;
  5.  
  6.  
  7. Var
  8.  Answer : String;
  9.  aPos      : Integer;
  10.  Item      : String = 'Now is the time for all good men';
  11.  
  12. begin
  13. Answer := ExtractWordPos(4,Item,[' '] , aPos);
  14. writeln(answer,'  ',apos);
  15. writeln('Press return to end . . .');
  16.  
  17. readln;
  18. end.

result
time  12
Press return to end . . .
   

 
   

Title: Re: ExtractWordPos ?
Post by: lucamar on March 20, 2019, 04:10:40 am
But if I make the deliminator more than one character I get an error.
So, just one space as deliminator.

WordDelims is a TSysCharSet, i.e. a set of AnsiChar; which means you can use, say:
Code: [Select]
FoundWord := ExtractWordPos(N, SrcStr, [' ', ',', '.', ':', ';'], APos);but, of course, not something like:
Code: [Select]
FoundWord := ExtractWordPos(N, SrcStr, [':::', '=='], APos);
Title: Re: ExtractWordPos ?
Post by: JLWest on March 20, 2019, 04:38:55 am
Thank you;

TinyPortal © 2005-2018