Count number of occurrences of a string
Printed From: Crystal Reports Book — Forum Name: Technical Questions
Posted By: saoco77 — 2 Sep 2009 at 6:27am
I'm trying to count the number of times the word "Title" occurs within a memo field (500-2000 chars).
Is this possible to do within Crystal??
Thanks Edited by saoco77 - 02 Sep 2009 at 6:29am
Posted By: lockwelle — 2 Sep 2009 at 11:01am
you would use a loop:
local numbervar iIndex := instr({table.field}, "Title");
local numbervar iCount:=0;
while iIndex <> 0 do
(
if iIndex <> 0 then iCount := iCount + 1;
iIndex = instr(iIndex + 1, {table.field}, "Title")
)
iCount
Posted By: saoco77 — 2 Sep 2009 at 1:28pm
I'm receiving the error message
A loop was evaluated more than the maximum number of times allowed.
If the word Title is not contained within the field the formula works fine.
Posted By: lockwelle — 2 Sep 2009 at 4:57pm
oops,
should have typed:
iIndex := instr(iIndex + 1, {table.field}, "Title")
need to assign the variable, not compare.
there should be a ; after the the last ) as well. Edited by lockwelle - 02 Sep 2009 at 4:59pm
Posted By: saoco77 — 3 Sep 2009 at 7:57am
I still couldn't get your formula to work. I think there may be an issue with the stop mechanism on the loop.
Listed below is the solution I finally came up with which breaks the process into two formulas.
@formula1
stringvar array SplitText := split({table.field}, " ");
stringvar array FilterText := filter(SplitText, "Letter");
stringvar iJoin;
iJoin := Join(FilterText,"");
iJoin
@formula2
evaluateafter({@formula1});
if isnull({@formula1}) then 0 else
(length({@formula1})/6)
Thank you again for your assistance, it is appreciated. Edited by saoco77 - 03 Sep 2009 at 7:57am
Posted By: lockwelle — 4 Sep 2009 at 6:19am
looks like a nice solution, but I thought you were looking for "Title"?
I have never seen the filter function before, but it looks like it would do the trick.
Posted By: Aliqux — 26 May 2016 at 5:12am
I know this is an older post but I thought this might help for other people looking for this. The code above helped me come up with this solution.
stringvar array textcheck := split({table.field}, "TEXT");
count(textcheck)-1;
Change the TEXT to what your looking for and the table will be split by that text. Each item in the array will then have the text your looking for. Subtract by one because the first split will be the first section without the word.
I ended up coming to this because there were some fields that the users were using commas instead of spaces to sperate the items Edited by Aliqux - 26 May 2016 at 5:14am
|