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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting characters in an output

Status
Not open for further replies.

rosn459

Programmer
Sep 18, 2003
37
US
I have an output that contains hyphens. I could have an instance of one hyphen or more than one.

EX:
AK-AL-AB
MO-MS
KS

How would I count the number of hyphens in my output? I know what I want to do once I get the correct number of hyphens.
 
how about using ListLen like so:
Code:
<cfoutput>
<cfset stringToSearch = "ab-cd-ef">
<cfset numofoccurances = ListLen(#stringToSearch#, '-')>
#Variables.numofoccurances#
</cfoutput>

will output 3, not the number of hyphens but the number of abbreviations.
 
Good Job NorthStar, that's the best way to do it..

But as has been discussed in thread232-833069 there's some issue with the above code when delimiters repeat..

If you want to count hyphens and you can be certain you'll never have two or more hyphens in a row, you'll be fine.

But if you can't be certain of that.. Here's your number of hyphens

Code:
<cfset hyphnum=len(string)-len(replace(string,"-","","ALL"))>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top