Here you can find the source of formatString(Object[] args)
Parameter | Description |
---|---|
args | a parameter |
public static String formatString(Object[] args)
//package com.java2s; import java.text.MessageFormat; import java.util.Locale; public class Main { /**//from ww w. ja v a 2s . c o m * This method will format the message contained in the first element of the * supplied array in the default locale * * @param args * @return formatted text */ public static String formatString(Object[] args) { return formatString(args, null); } /** * This method assumes that the actual message to be formatted is the first * element of the supplied array. If the supplied array has only one element * that element is the message * * @param args * @param locale or null if the default should be used * @return */ public static String formatString(Object[] args, Locale locale) { if (args == null || args.length < 1) { return null; } Object msg = args[0]; if (msg == null || !(msg instanceof String)) { return null; } String sMsg = (String) msg; final int len = args.length; if (len < 2) { return sMsg; } final int argsLen = len - 1; Object[] fmtArgs = new Object[argsLen]; System.arraycopy(args, 1, fmtArgs, 0, argsLen); return formatString(sMsg, fmtArgs); } /** * Format the supplied message in the specified locale * * @param message * @param args * @return formatted text */ public static String formatString(String message, Object[] args) { return formatString(message, args, null); } /** * A utility method to format the supplied message using MessageFormat * * @param message * @param args * @param locale may be null * @return formatted text */ public static String formatString(String message, Object[] args, Locale locale) { MessageFormat formatter = null; if (locale != null) { formatter = new MessageFormat(message, locale); } else { formatter = new MessageFormat(message); } String formattedText = formatter.format(args); return formattedText; } }