[fpc-pascal] Split stream into words

Marco van de Voort marcov at stack.nl
Tue Jul 3 12:46:05 CEST 2018


In our previous episode, Michael Van Canneyt said:
> 
> > In our previous episode, Michael Van Canneyt said:
> >> 
> >> What's the easiest way to split a stream into words ?
> >
> > Doesn't strutils have some word extraction and count functions?
> 
> It does: WordCount,ExtractWord, but they are very inefficent.


function splitstring(const s:string;c:char):TStringList;

var i,i2,j : integer;
    x : string;
begin
  result:=TStringlist.create;
  i:=0;
  repeat
    j:=PosEx(c,s,i+1);
    i2:=j;
    if i2=0 then i2:=length(s)+1;
    x:=trim(copy(s,i+1,i2-i-1));
    result.add(x);
    i:=j;
  until j=0;
end;

Afaik I also must have a variant with posset somewhere. In another variant
I use a class around a array of string, which keeps a count of valid
entries. This avoids setlengths on repeated use. All fairly trivial.



More information about the fpc-pascal mailing list