メソッド format()は、フォーマット String と引数を使用して String をフォーマットします。たとえば、引数 arg__がNULLの場合、文字 's’および 'S’は「NULL」と評価されます。
arg がFormattableを実装している場合は、メソッド Formattable、 、メソッド arg.formatTo()が呼び出されます。それ以外の場合は、 arg.toString()__を呼び出して結果が評価されます。
フォーマットの詳細については、https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html[Javadoc]を参照してください。
利用可能な署名
public static String format(String format, Object... args)
public static String format(Locale l, String format, Object... args)
例
@Test
public void whenFormat__thenCorrect() {
String value = "Baeldung";
String formatted = String.format("Welcome to %s!", value);
assertEquals("Welcome to Baeldung!", formatted);
}
投げる
-
IllegalFormatException - フォーマット String に無効な値が含まれている場合
構文。
@Test(expected = IllegalFormatException.class)
public void whenInvalidFormatSyntax__thenIllegalFormatExceptionThrown() {
String value = "Baeldung";
String formatted = String.format("Welcome to %x!", value);
}
次 "
-
«** 前へ