Search code examples
crystal-reports

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


Solution

  • 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]," ")