CFLib.org – Common Function Library Project

CompareIgnoreWhiteSpace(string1, string2 [, isCompareCaseSensitive])

Last updated December 2, 2008

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

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson