CFLib.org – Common Function Library Project

differentChars(string [, caseSensitive])

Last updated February 6, 2004

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 1 time(s). Average Rating: 1.0

Description:
Counts how many different chars are in a string. Usable for passwords where you can ensure there is at least x different chars, can check case sensitive and not.

Return Values:
Returns a number.

Example:

view plain print about
<cfset mystring = "aAbBcC">
<cfoutput>
Not case sensitive: #differentChars(mystring)#<br>
Case sensitive: #differentChars(mystring,true)#
</cfoutput>

Parameters:

Name Description Required
string String to check. Yes
caseSensitive Determines if case sensitivity is used. Defaults to false. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Counts how many different chars are in a string.
 * removed use of arguments. to make it cf5 compat
 * 
 * @param string      String to check. (Required)
 * @param caseSensitive      Determines if case sensitivity is used. Defaults to false. (Optional)
 * @return Returns a number. 
 * @author Bjorn Jensen (public.cflib@saghian.com) 
 * @version 1, February 6, 2004 
 */

function differentChars(string){
    var iCount = 0;
    var i = 0;
    var sChars = "";
    var sChar = "";
    var caseSensitive = false;

    if (arrayLen(arguments) eq 2 and isBoolean(arguments[2]) and arguments[2]) {
        caseSensitive = true;
    }
    
    for(i=1;i lte len(string);i=i+1){
        sChar = mid(string, i, 1);
        if (caseSensitive and not find(sChar, sChars)){
            sChars = sChars & sChar;
            iCount = iCount+1;
        } else if (not caseSensitive and not findNoCase(sChar, sChars)){
            sChars = sChars & sChar;
            iCount = iCount+1;
        }
    }

    return iCount;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson