Is there a format function available in a Lightning Component controller or helper?

Would prefer a platform function or a way to get to the platform function so please answer if you have a way to do that.

But for now I have added this to my helper:

format: function(string) {
    var outerArguments = arguments;
    return string.replace(/\{(\d+)\}/g, function() {
        return outerArguments[parseInt(arguments[1]) + 1];
    });
},

which is a slight tweak of the accepted answer to MessageFormat in javascript (parameters in localized UI strings) so that the substitution values don't have to be wrapped in an array.

PS

Better to use the build-in format function - see How to add dynamic variable to custom label in lightning javascript helper class.


Salesforce have initially exposed the support for $A.util.format() in their documentation by mistake.

Then they later removed it from their documentation. This code will not compile from api version 40 or later.

Here is another related post

Currently recommended method seem to be to use javascript replace() function. It would be really good if Salesforce can enable this functionality natively, but for the moment we'll have to live this :(