Apex API String.format(); Exploring Good & Bad Parts!

I recently came across this useful and under-documented Apex String API, i.e.

String formattedString = String.format(String template, List<String> arguments)

String.format(..) is again based on the Java MessageFormat class; its documentation says:

Treat the current string as a pattern that should be used for substitution in the same manner as apex:outputText.

The nice part of this String API is that it supports basic text substitution, for example:

// Create a Template String, that has tokens of form {index} 
String templateString = 'Hello {0}, Good to see you in {1}';
// String argument list or array matching each {index} in Template String.
String[] arguments = new String[] {'Ram' , 'India'};
// Call String.format() to get the token replaced
String formattedString = String.format(templateString, arguments);
System.debug(formattedString);
// output : Hello Ram, Good to see you in India

The bad part about this String API is that it only supports String arguments for token replacement. Though both Java’s MessageFormat and Visuaforce's apex:outputText tag accept non-string arguments like Date, Datetime, and Currency, and are capable of doing very smart formatting with them. For example, in VisualForce, one can format a date using outputText as follows:

Code Snippet Display

<apex:outputText value="The formatted time right now is: 
    {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}">
   <apex:param value="{!NOW()}" />
</apex:outputText>

Output: The formatted time right now is: 2004.11.20 AD at 23:49:02 GMT
        

A similar Java example with more options would be:

Object[] arguments = {
     new Integer(7),
     new Date(System.currentTimeMillis()),
     "a disturbance in the Force"
 };

 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     arguments);

 output: At 12:30 PM on Jul 3, 2053, there was a disturbance
           in the Force on planet 7.

Sadly, if you try to do something similar with Apex, it always fails for a StringException.

Expected change in Apex String.format () API

Apex String.format (String pattern, String[] arguments) API, should take Object[] as an argument instead of String[]. This will help support more primitive data types, i.e., datatime, date, currency, and number. Just like visual force apex:outputText tag, this change would be safe and backward compatible, as all existing Apex code using String[] should work with Object[] too.

Resources

Let’s Talk

CTA Banner

Drop a note below to move forward with the conversation 👇🏻

Abhinav Gupta

First Indian Salesforce MVP, rewarded Eight times in a row, has been blogging about Salesforce, Cloud, AI, & Web3 since 2011. Founded 1st Salesforce Dreamin event in India, called “Jaipur Dev Fest”. A seasoned speaker at Dreamforce, Dreamin events, & local meets. Author of many popular GitHub repos featured in official Salesforce blogs, newsletters, and books.

https://abhinav.fyi
Previous
Previous

Javascript Remoting & jQuery Templates: An Easy Way to Rich and High-Performance Interfaces!

Next
Next

Invoking “Apex WSDL” Web Services from Apex Code