[Java SE] 字符串连接

Java 支持多种字符串连接方式,总结如下:

package cn.spads.tool.string;

import java.text.MessageFormat;

/**
 * <b>测试字符串连接</b><br>
 * @author		Surmounting
 * @version		V1.1.0
 * history
 * 1.1.0, 2015年9月1日		Surmounting			初版
 * @since		Java 6.0
 */
public class TestStringConnect {

	public static void main(String[] args) {

		// 使用字符串连接符
		System.out.println("今天是" + 2015 + "年" + 9 + "月" + 1 + "日");

		// 使用StringBuilder
		System.out.println(new StringBuilder("今天是").append(2015)
		        .append("年")
		        .append(9)
		        .append("月")
		        .append(1)
		        .append("日"));

		// 使用StringBuffer
		System.out.println(new StringBuffer("今天是").append(2015).append("年").append(9).append("月").append(1).append("日"));

		// 使用String.format
		System.out.println(String.format("今天是%s年%s月%s日", 2015, 9, 1));

		// 使用MessageFormat.format
		System.out.println(MessageFormat.format("今天是{0}年{1}月{2}日", "2015", "9", "1"));

		// 使用String.concat 只能连接字符串
		System.out.println("今天是".concat("2015").concat("年").concat("9").concat("月").concat("1").concat("日"));

	}
}

  

posted @ 2015-09-01 09:46  driftingshine  阅读(207)  评论(0编辑  收藏  举报