Java String Format format(final String message, final Object... args)

Here you can find the source of format(final String message, final Object... args)

Description

format

License

Apache License

Declaration

public static String format(final String message, final Object... args) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    public static String format(final String message, final Object... args) {
        try {//from w ww. ja  v a2  s  .c o m
            for (int i = 0; i < args.length; i++) {
                final Object o = args[i];
                if (o != null && o.getClass().isArray()) {
                    args[i] = Arrays.asList((Object[]) o);
                }
            }
            return String.format(message, args);
        } catch (final Exception e) {
            final String error = buildExceptionMessage(message, e, args);
            return error;
        }
    }

    private static String buildExceptionMessage(final String message, final Exception e, final Object... args) {
        String error = "Could not format message with format string: {" + message + "}, args: {";
        final boolean useComma = false;
        for (final Object arg : args) {
            if (useComma) {
                error += ", ";
            }
            error += "{" + (arg != null ? arg : "null") + "}";
        }
        return error;
    }
}

Related

  1. createFormatter()
  2. format(final String s, final int width, final int intend)
  3. format(String longMessage, int charPerLine, int paddingLeft, boolean padFirstLine)
  4. format(String name, char separator)
  5. format(String name, char separator, String prefix, boolean includePrefix, boolean includeLeadingSeparator)