Java Locale Format formatString(Object[] args)

Here you can find the source of formatString(Object[] args)

Description

This method will format the message contained in the first element of the supplied array in the default locale

License

Open Source License

Parameter

Parameter Description
args a parameter

Return

formatted text

Declaration

public static String formatString(Object[] args) 

Method Source Code


//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;
    }
}

Related

  1. formatNumberUk(double inNumber, int inDecimalPlaces)
  2. formatNumberWithThousandSeparator(long number)
  3. formatPCT(Object num)
  4. formatPersent(Object input)
  5. formatSmartDate(Date date)
  6. formatString(String pattern, Object... args)
  7. formatString2Date(String dateString)
  8. formattedFromDouble(double d, int scale, Locale locale)
  9. formattedFromLong(long l, Locale locale)