CFLib.org – Common Function Library Project

stringFormat(str, msk)

Last updated August 10, 2007

author

Joshua Miller

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Formats an alpha-numeric string to match a given mask. Useful for displaying phone numbers, IP addresses, codes, etc.

Return Values:
Returns a string.

Example:

stringFormat("12ABC01","XX-XXX.XX")

Parameters:

Name Description Required
str String to format. Yes
msk Mask to use. Yes

Full UDF Source:

/**
 * Formats a string similarly to how NumberFormat formats a number.
 * 
 * @param str 	 String to format. (Required)
 * @param msk 	 Mask to use. (Required)
 * @return Returns a string. 
 * @author Joshua Miller (jmiller@garrisonenterprises.net) 
 * @version 1, August 10, 2007 
 */
function stringFormat(str,msk){
	var i=0;
	var c=1;
	var r="";
	for(i=1;i LTE Len(msk);i=i+1){
		if(Find("X",mid(msk,i,1)) GT 0){
			if(c LTE Len(str)){
				r = r & mid(str,c,1);
				c = c + 1;
			}else{
				r = r & " ";
			}
		}else{
			r = r & mid(msk,i,1);
		}
	}
	return r;
}
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