Andy K
Andy K

Reputation: 5075

Crystal Reports find the first 3 words of a string

I have a database field with strings like this

oh the sea OToole was right
I like ramen but
Rowing like a Blue but not an artist

They are actual words separated with space

I want to find to extract the first 3 words

The result would be like below

oh the sea 
I like ramen 
Rowing like a 

I tried the following

 ExtractString({tbname.field1},""," ") & " " & ExtractString({tbname.field1}," "," ") & ExtractString({tbname.field1},"   "," ")

It did work for the the first two fields but not the second

I tried the one below too

split({tbname.field1}, " ")[1] & " " & split({tbname.field1}, " ")[2] 
& " " & split({tbname.field1}, " ")[3] 

It gives me an error saying the indice must be between 1 and the size of the array

Any insights are more than welcome

Upvotes: 0

Views: 1078

Answers (2)

craig
craig

Reputation: 26272

** edited to reflect data is contained in a single row, rather than 3 separate rows

Try:

// defined delimiter
Local Stringvar CRLF := Chr(10)+Chr(13);

// split CRLF-delimited string into an array
Local Stringvar Array rows := Split({Command.WORDS}, CRLF);

// the results of all the work
Local Stringvar Array results;

// process each 'row'
Local Numbervar i;
for i := 1 to ubound(rows) do (
  // increment the array, then add first 3 words
  Redim Preserve results[Ubound(results)+1];
  results[ubound(results)]:=Join(Split(rows[i])[1 to 3]," ")
);

// create CRLF-delimited string
Join(results, CRLF);

Upvotes: 1

CoSpringsGuy
CoSpringsGuy

Reputation: 1645

if ubound(Split({tbname.field1})) < 3 then
Join(Split({tbname.field1})[1 to ubound(Split({tbname.field1}))]," ")
else Join(Split({tbname.field1})[1 to 3]," ")

Upvotes: 1

Related Questions