Print Page | Close Window

Count number of occurrences of a string

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=7566
Printed Date: 01 May 2024 at 7:03pm


Topic: Count number of occurrences of a string
Posted By: saoco77
Subject: Count number of occurrences of a string
Date Posted: 02 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



Replies:
Posted By: lockwelle
Date Posted: 02 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
Date Posted: 02 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
Date Posted: 02 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.


Posted By: saoco77
Date Posted: 03 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.


Posted By: lockwelle
Date Posted: 04 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
Date Posted: 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



Print Page | Close Window