方式一
String.format()

static String str = "%s和%s的%s.一刻也不能%s,無論%s走到那里,都留下一首贊歌"; public static void main(String[] args) { String format = String.format(str, "我", "我", "祖國", "分隔", "我","啦啦啦的馬蒂"); System.out.println(format); //輸出結果:我和我的祖國.一刻也不能分隔,無論我走到那里,都留下一首贊歌 }
方式二
MessageFormat.format()

import java.text.MessageFormat; static String str = "{0}和{0}的{1}.一刻也不能{2},無論{0}走到那里,都留下一首贊歌"; public static void main(String[] args) { String format = MessageFormat.format(str, "我", "祖國", "分隔", "啦啦啦啦的馬蒂"); System.out.println(format); //輸出:我和我的祖國.一刻也不能分隔,無論我走到那里,都留下一首贊歌 }
================================附錄1:String.format()詳解使用=============================================
參考地址:https://blog.csdn.net/anita9999/article/details/82346552