CFLib.org – Common Function Library Project

IsPalindrome(string)

Last updated November 22, 2001

author

Douglas Williams

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Checks string to see if it is a palindrome. A palindrome is any word or phrase that is the same spelled forwards or backwards. The test performed will ignore spaces and punctuation.

Return Values:
Returns a boolean.

Example:

<cfset mystr = "racecar">
<cfset complex = "Sums are not set as a test on Erasmus">
<cfset bad = "This will not work.">

<cfoutput>
Is "racecar" a Palindrome: #isPalindrome(mystr)#<BR>
Is "Sums are not set as a test on Erasmus" a Palindrome: #isPalindrome(complex)#<BR>
Is "This will not work." a Palindrome: #isPalindrome(bad)#<BR>
</cfoutput>

Parameters:

Name Description Required
string The string to check. Yes

Full UDF Source:

/**
 * Checks string to see if it is a pPalindrome.
 * Modified by Raymond Camden
 * 
 * @param string 	 The string to check. 
 * @return Returns a boolean. 
 * @author Douglas Williams (klenzade@i-55.com) 
 * @version 1, November 22, 2001 
 */
function isPalindrome(string) {
  var newstring = rereplacenocase(string, '[^a-z1-9]','', 'ALL');
  return newstring eq reverse(newstring);
}
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