首页 > 编程语言 > 详细

[Java SE] 字符串连接

时间:2015-09-01 10:27:32      阅读:231      评论:0      收藏:0      [点我收藏+]

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("日"));

	}
}

  

[Java SE] 字符串连接

原文:http://www.cnblogs.com/driftingshine/p/4774908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!