Jump to content
sjesper

GetWords

Recommended Posts

This function return the words in a string. This is only adviced to be used with 100-150 words, or it will take long time to get the words (This can ofc be used as a kind of "wait" in your script).

 

10 words ~ 54 ms (0.054 seconds)

50 words ~ 277 ms (0.277 seconds)

100 words ~ 700 ms (0,700 seconds)

 

[sCAR]function GetWords(S: String): TStringArray;

var

Len, i, ii, Index, L: Integer;

S2: String;

TSA: TStringArray;

begin

L := Length(S);

TSA := Explode(' ', S);

for ii := 0 to high(TSA) do begin

for i := low(TSA) to high(TSA) do begin

Index := Pos(TSA, S);

Len := PosEx(' ', S, Index);

S2 := Copy(S, Len + 1, 1);

if S2 = ' ' then

Delete(S, Len, 1);

end;

end;

for i := 1 to L do begin

if i = 1 then

if not StartsWith(' ', S) then

break;

Len := Pos(' ', S);

if Len = 1 then begin

Delete(S, Len, 1);

end else begin

break;

end;

end;

result := Explode(' ', S);

end;

 

var

i: Integer;

TSA: TStringArray;

 

begin

TSA := GetWords('Hello Hello Hello Hello Hello Hello Hello Hello Hello HelloHello Hello HelloHello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello HHello Hello Hello Hello Hello Hello o Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hell Hello H');

for i := 0 to high(TSA) do

writeln('TSA[' + IntToStr(i) + '] = ' + TSA);

end.[/sCAR]

Link to comment
Share on other sites

That's too slow, but a good practice.

 

I'd use the built-in Explode to get words out of a string.

 

1000 words ~ 0ms

5000 words ~ 78ms

 

[sCAR]

program New;

 

var

i, start: Integer;

s: String;

tsa: TStringArray;

 

begin

// make a 5000 word long string

for i := 1 to 5000 do

s := s + 'hello ';

start := GetSystemTime;

tsa := Explode(' ', s);

WriteLn(IntToStr(Length(tsa)) + ' words ~ ' + IntToStr(GetSystemTime-start) + 'ms');

end.

[/sCAR]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...