Crystal Reports查找字符串的前3个单词

时间:2016-02-22 15:09:11

标签: crystal-reports

我有一个像这样的字符串的数据库字段

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

它们是用空格分隔的实际单词

我想找到提取前3个单词

结果如下

oh the sea 
I like ramen 
Rowing like a 

我尝试了以下

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

它确实适用于前两个字段,但不适用于第二个字段

我也试过了下面的那个

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

它给出了一个错误,指出indice必须介于1和数组的大小之间

任何见解都非常受欢迎

2 个答案:

答案 0 :(得分:1)

**编辑以反映数据包含在一行中,而不是3行单独的行

尝试:

// 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);

答案 1 :(得分:1)

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