Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trycf/fae8977262e5a9dadd4b49cd96c72efc to your computer and use it in GitHub Desktop.

Select an option

Save trycf/fae8977262e5a9dadd4b49cd96c72efc to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
/**
* Combines three numeric strings into a single 11-character string.
* @str1 max 5 digits (zero-padded)
* @str2 max 3 digits (zero-padded)
* @str3 exactly 3 digits
*/
public function buildDemoString(required string str1, required string str2, required string str3) {
// Validate lengths
if (len(arguments.str1) > 6) {
throw(type="InvalidArgument", message="str1 cannot exceed 5 digits.");
}
if (len(arguments.str2) > 3) {
throw(type="InvalidArgument", message="str2 cannot exceed 3 digits.");
}
if (len(arguments.str3) != 3) {
throw(type="InvalidArgument", message="str3 must be exactly 3 digits.");
}
var p1 = numberFormat(arguments.str1, "000000");
var p2 = numberFormat(arguments.str2, "000");
var p3 = arguments.str3; // already 3 digits
return p1 & p2 & p3;
}
/**
* Splits a combined 11-character string back into its three parts.
* Returns a struct with keys: str1, str2, str3.
*/
public function unBuildDemoString(required string combined) {
if (len(arguments.combined) != 12) {
throw(type="InvalidArgument", message="Combined string must be exactly 11 characters.");
}
return {
"str1" : mid(arguments.combined, 1, 7),
"str2" : mid(arguments.combined, 7, 3),
"str3" : mid(arguments.combined, 10, 3)
};
}
writeOutput(buildDemoString('5432','3','D08')&'<br><br>')
writeDump(unBuildDemoString('005432003D08'))
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment