The following function can be used to get the number of words in a string.
Call the function like ShowMessage(IntToStr(GetWordCount('test string, 1 2 3')));
1 2 //Function starts here3 function GetWordCount(const SourceStr: string): Longint;
4 var5 i, len: Longint;
6 WordSeparators: string;
7 begin8 Result := 0;
9 WordSeparators := ' ,.;'; { If needed then add more string separators }10 i := 1;
11 len := Length(SourceStr); { Get the length of source string and assign to a 12 variable }13 while i <= len do14 begin15 if Pos(SourceStr[i], WordSeparators) = 0 then16 { i.e currenct char is a valid word character like a, b, c etc. }17 begin18 { Enter a loop that will end if a word separator charcter like ., space 19 etc. found }20 while (i <= len) and (Pos(SourceStr[i], WordSeparators) = 0) do21 Inc(i);
22 Inc(Result);
23 end;
24 Inc(i);
25 end;
26 end;