replacecoldfusioncharactercfoutput

Remove characters in <cfoutput> with ReplaceNoCase() ColdFusion


I need to display an output from a data record that is formatted similar to this: XXXX:12345 (Xxxxxxxxx)

However, the only data I want to output is the "12345" and with two preceding zeros, i.e. the output should look like "0012345". The "12345" in the record is example only, each record has a unique number assigned. An example record looks like this: CAST:98765 (RPOS1234-XY)

Can I use the ReplaceNoCase() to pull only that data out of the record? If so, how would I write the code to remove the unwanted characters?


Solution

  • You can do this in one line of code using a few functions.

    str = 'CAST:98765 (RPOS1234-XY)';
    projectCode = '00' & listLast( listFirst( str, ' ' ), ':' );
    
    writeDump( projectCode );
    

    To explain this code from the inner most function going out.

    ListFirst() gets the first element in an a list based on the delimiter you specify, in this case the delimiter is ' ' - a space - yes, you can use a space as a delimiter.

    ListLast() gets the last element in a list based on the delimiter you specify, in this case the delimiter is ':'

    The first part simplt appends '00' to the result of the above function calls.