Index: modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java =================================================================== --- modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java (revision 430666) +++ modules/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/String2Test.java (working copy) @@ -893,6 +905,19 @@ // Test for method java.lang.String java.lang.String.contentEquals(CharSequence cs) assertFalse("Incorrect result of compare", "qwerty".contentEquals("")); } + + /** + * @tests java.lang.String#format(Locale, String, Object[]) + */ + public void test_format() { + assertEquals("13% of sum is 0x11", + String.format("%d%% of %s is 0x%x", 13, "sum", 17)); + assertEquals("empty format", "", String.format("", 123, this)); + try { + String.format(null); + fail("NPE is expected on null format"); + } catch (NullPointerException ok){} + } /** * Sets up the fixture, for example, open a network connection. This method Index: modules/luni/src/main/java/java/lang/String.java =================================================================== --- modules/luni/src/main/java/java/lang/String.java (revision 430666) +++ modules/luni/src/main/java/java/lang/String.java (working copy) @@ -18,6 +18,7 @@ import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.util.Comparator; +import java.util.Formatter; import java.util.Locale; import java.util.regex.Pattern; @@ -1965,6 +2008,44 @@ return r - offset; } + /** + * Returns a printf-style formatted string, using the supplied format and + * arguments. This function is a shortcut to + * format(Locale.getDefault(), format, args). + * @param format a format string + * @param args arguments to replace format specifiers, may be none + * @throws NullPointerException if the format is null + * @throws IllegalArgumentException if the format is invalid + * @return The formatted string + * @since 1.5 + * @see java.util.Formatter + */ + public static String format(String format, Object... args) { + return format(Locale.getDefault(), format, args); + + } + + /** + * Returns a printf-style formatted string, using the supplied format and + * arguments, accordingly to the specified locale. + * @param loc the locale to apply; null value means no localization + * @param format a format string + * @param args arguments to replace format specifiers, may be none + * @throws NullPointerException if the format is null + * @throws IllegalArgumentException if the format is invalid + * @return The formatted string + * @since 1.5 + * @see java.util.Formatter + */ + public static String format(Locale loc, String format, Object... args) { + if (format == null) { + throw new NullPointerException("null format argument"); + } + int bufferSize = format.length() + (args == null ? 0 : args.length * 10); + Formatter f = new Formatter(new StringBuilder(bufferSize), loc); + return f.format(format, args).toString(); + } + /* * An implementation of a String.indexOf that is supposed to perform * substantially better than the default algorithm if the the "needle" (the