CFLib.org – Common Function Library Project

GetAlphabetPosition(charornum)

Last updated January 07, 2002

author

Seth Duffey

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Returns the numeric value of a letter's position in the alphabet, or returns the matching letter of a number in the alphabet.

Return Values:
Returns either a character, number, or empty string on error.

Example:

<cfoutput>
The 3rd letter in the alphabet is #GetAlphabetPosition(3)#<br>
The 26th letter in the alphabet is #GetAlphabetPosition(26)#<br>
"M" is the #GetAlphabetPosition("M")#(th/rd) letter in the alphabet.<br>
"Z" is the #GetAlphabetPosition("Z")#(th/rd) letter in the alphabet.<br>
</cfoutput>

Parameters:

Name Description Required
charornum Either a character or number. Yes

Full UDF Source:

/**
 * Returns the numeric value of a letter's position in the alphabet, or the returns matching letter of a number in the alphabet.
 * 
 * @param charornum      Either a character or number. 
 * @return Returns either a character, number, or empty string on error. 
 * @author Seth Duffey (sduffey@ci.davis.ca.us) 
 * @version 1, January 7, 2002 
 */
function GetAlphabetPosition(charornum) {
  var a_numeric = asc("a");
  charornum = lCase(trim(charornum));

  if(isNumeric(charornum)) {
      if(charornum lte 0 OR charornum gte 27) return "";
      return chr(charornum+a_numeric-1);
  } else {
      if(len(charornum) gt 1) return "";
      if(REFind("[^a-z]",charornum)) return "";
      return asc(charornum) - a_numeric + 1;
  }
  return 1;
}

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