String.format和MessageFormat.format的对比用法

1.MessageFormat.format

import java.text.MessageFormat;

/**
 * Created by SYJ on 2017/9/13.
 */
public class MainTest {
    public static void main(String[] args) {
        String url = "http://xxx.xxx.xxx?name={0}&age={1}";
        String path = MessageFormat.format(url, "张三", 18);
        System.out.println(path);//http://xxx.xxx.xxx?name=张三&age=18
    }
}

2.String.format

/**
 * Created by SYJ on 2017/9/13.
 */
public class MainTest {
    public static void main(String[] args) {
        String url = "http://xxx.xxx.xxx?name=%s&age=%d";
        String path = String.format(url, "张三", 18);
        System.out.println(path);//http://xxx.xxx.xxx?name=张三&age=18
    }
}

 

posted @ 2017-09-22 18:15  xuebusi  阅读(1092)  评论(0编辑  收藏  举报