CFLib.org – Common Function Library Project

CompareIgnoreWhiteSpace(string1, string2[, isCompareCaseSensitive])

Last updated December 02, 2008

author

Mark G. Smith

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Performs a comparison of two strings ignoring any whitespace characters including form feeds, line feeds, carriage returns, tabs, vertical tabs, and spaces. Comparison is case sensitive unless a third argument of "false" is passed in.

Return Values:
Returns a boolean.

Example:

<cfset str1 = "The quick brown fox jumps over the lazy dog.">
<cfset str2 = "The quick brown     fox jumps over the lazy     dog.">
<cfset strChange = CompareIgnoreWhiteSpace(str1,str2)>
<cfif strChange eq 0>
    The strings are equal!
<cfelse>
    The strings are NOT equal!
</cfif>

Parameters:

Name Description Required
string1 First string to compare. Yes
string2 Second string to compare. Yes
isCompareCaseSensitive Determines if the comparison should check case. Default is true. No

Full UDF Source:

/**
 * Performs a comparison of two strings ignoring any whitespace characters.
 * 
 * @param string1      First string to compare. (Required)
 * @param string2      Second string to compare. (Required)
 * @param isCompareCaseSensitive      Determines if the comparison should check case. Default is true. (Optional)
 * @return Returns a boolean. 
 * @author Mark G. Smith (mark.g.smith@baesystems.com) 
 * @version 1, December 2, 2008 
 */
function CompareIgnoreWhiteSpace(string1,string2) {
    var string1NoWhiteSpace = REReplace(string1,"[\s]","","ALL");
    var string2NoWhiteSpace = REReplace(string2,"[\s]","","ALL");
    var isCompareCaseSensitive = true;
    
    if(arrayLen(arguments) gte 3) isCompareCaseSensitive = arguments[3];
    
    if (isCompareCaseSensitive)
        return Compare(string1NoWhiteSpace, string2NoWhiteSpace);
    else
        return CompareNoCase(string1NoWhiteSpace, string2NoWhiteSpace);
}

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