zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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

string 用法 对比 format
2023-09-27 14:25:06 时间

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
    }
}