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;
}

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