CFLib.org – Common Function Library Project

differentChars(string[, caseSensitive])

Last updated February 6, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson