1. 程式人生 > >Java 占位符

Java 占位符

ring 使用 java tex ava %d alt port 類對象

Java的占位符有兩種:% 和 {}

String 類對象 只能使用 % 有效。

MessageFormat 類對象 只能使用 {} 有效。

package demo;

import java.text.MessageFormat;


public class doTest {
    public static void main(String[] args) throws Exception {
        System.out.println(String.format("Input = {0} and Output = {1}", 1,2));
        System.out.println(MessageFormat.format("Input = {0} and Output = {1}", 1,2));
        System.out.println();
        System.out.println(String.format("Input = %d and Output = %d", 1,2));
        System.out.println(MessageFormat.format("Input = %d and Output = %d", 1,2));    
    }
}

運行結果

技術分享圖片

Java 占位符