CFLib.org – Common Function Library Project

GetAlphabetPosition(charornum)

Last updated January 7, 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;
}
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