CFLib.org – Common Function Library Project

differentChars(string[, caseSensitive])

Last updated February 06, 2004

author

Bjorn Jensen

Version: 1 | Requires: CF5 | Library: StrLib

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:

<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:

/**
 * 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson