Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instr AND Count 2

Status
Not open for further replies.

dgillz

Instructor
Mar 2, 2001
10,045
US
Instr(subtring,string) will return an integer indicating where the subtring exists in the string. I am looking for a COUNT of how many times the substring appears in the string.

Is there a function that does this? If not how would I do this with a formula? I worked on this for an hour yesterday to no avail.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports
 
You would need a for loop, something like this


Local NumberVar str := 0;
Local NumberVar strLen := Length ({YourField});
Local NumberVar i;
For i := 1 To strLen Do
(
if instr(i, {YourField}, 'Somevalue') <> 0 then
str := str + 1;
);
str

Mo
 
I know I know, there's an error. I am working at it but its a start

Mo
 
This should work better

Local NumberVar str := 0;
Local NumberVar strLen := Length ({TBL.Field});
Local NumberVar i;
For i := 1 To strLen Do
(
if Mid({TBL.Field}, i, 1) = YourValue then
str := str + 1;
);
str

no need for the InStr function

Mo
 
I like your approach, however:

Mid({TBL.Field}, i, 1)

Will always return a 1 character string. My susbstring I am searching for could be more than 1 character.



Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports
 
No it won't because you can validate for more than 1 character at the time

Mo
 
I think you might be talking a little bit at cross purposes.

dgillz means that your formula is cool for single character searches, but the loop always assumes a length of 1, so will return 0 for substrings > 1.

Sex it up a bit:
Code:
Local NumberVar str := 0;
Local NumberVar strLen := Length ({TBL.Field});
Local NumberVar i;
Local StringVar x := 'whatever';

For i := 1 To strLen Do
(
if Mid({TBL.Field}, i, Length(x)) = x then
str := str + 1;
);
str
Naith
 
Perfect Naith. I knew this would require a Do loop but I am not too proficient in this area.

Thanks again.

Software Sales, Training, Implementation and Support for Macola, eSynergy, and Crystal Reports
 
MisterMo, I "*"ed you seeing as you did all the legwork. ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top