JavaScript 的 StringBuilder
今天用JS 写了一个 类似于 C# 中的 StringBuilder 类, 可以在Javascript 中调用.用于处理大量的字符串拼接等,使得代码易读,易于维护.
首先引入 StringBuilder.js
<script type="text/javascript" src="StringBuilder.js"></script>
然后便可以很方便的使用了:
基本用法:
var sb=new StringBuilder();
sb.Append("Hello");
sb.Append("World !");
var Result=sb.ToString();
用法二:
代码
// sb.AppendFormat("{0}....{N}","a",...."N");
var sb2=new StringBuilder();
var a="西狐";
var b=21;
var c="男";
sb2.Append("welcome! ");
sb2.AppendFormat("我叫:{0},年龄:{1},性别:{2}",a,b,c);
var sb2=new StringBuilder();
var a="西狐";
var b=21;
var c="男";
sb2.Append("welcome! ");
sb2.AppendFormat("我叫:{0},年龄:{1},性别:{2}",a,b,c);
用法三:
代码
// sb.AppendFormat("{0}....{N}",Array);
var sb3=new StringBuilder();
//可与数组一一对应
var param=new Array();
param[0]="20";
param[1]="微风三级";
param[2]="东南风";
param[3]="阳光明媚";
param[4]=new Date().toLocaleDateString();
sb3.AppendFormat("今天:{4},温度:{0},风力:{1},风向:{2},{3}",param);
var sb3=new StringBuilder();
//可与数组一一对应
var param=new Array();
param[0]="20";
param[1]="微风三级";
param[2]="东南风";
param[3]="阳光明媚";
param[4]=new Date().toLocaleDateString();
sb3.AppendFormat("今天:{4},温度:{0},风力:{1},风向:{2},{3}",param);
完整的示例代码:
代码
<html>
<head>
<title>JavaScript StringBuilder Demo</title>
<script type="text/javascript" src="StringBuilder.js"></script>
<script type="text/javascript">
//Created By hooyes
//基本用法
var sb=new StringBuilder();
sb.Append("Hello");
sb.Append("World !");
var Result=sb.ToString();
document.write(Result);
document.write("<hr />");
//用法二
// sb.AppendFormat("{0}....{N}","a",...."N");
var sb2=new StringBuilder();
var a="西狐";
var b=21;
var c="男";
sb2.Append("welcome! ");
sb2.AppendFormat("我叫:{0},年龄:{1},性别:{2}",a,b,c);
document.write(sb2.ToString());
document.write("<hr />");
//用法三
// sb.AppendFormat("{0}....{N}",Array);
var sb3=new StringBuilder();
//可与数组一一对应
var param=new Array();
param[0]="20";
param[1]="微风三级";
param[2]="东南风";
param[3]="阳光明媚";
param[4]=new Date().toLocaleDateString();
sb3.AppendFormat("今天:{4},温度:{0},风力:{1},风向:{2},{3}",param);
document.write(sb3.ToString());
</script>
</head>
<body>
</body>
</html>
<head>
<title>JavaScript StringBuilder Demo</title>
<script type="text/javascript" src="StringBuilder.js"></script>
<script type="text/javascript">
//Created By hooyes
//基本用法
var sb=new StringBuilder();
sb.Append("Hello");
sb.Append("World !");
var Result=sb.ToString();
document.write(Result);
document.write("<hr />");
//用法二
// sb.AppendFormat("{0}....{N}","a",...."N");
var sb2=new StringBuilder();
var a="西狐";
var b=21;
var c="男";
sb2.Append("welcome! ");
sb2.AppendFormat("我叫:{0},年龄:{1},性别:{2}",a,b,c);
document.write(sb2.ToString());
document.write("<hr />");
//用法三
// sb.AppendFormat("{0}....{N}",Array);
var sb3=new StringBuilder();
//可与数组一一对应
var param=new Array();
param[0]="20";
param[1]="微风三级";
param[2]="东南风";
param[3]="阳光明媚";
param[4]=new Date().toLocaleDateString();
sb3.AppendFormat("今天:{4},温度:{0},风力:{1},风向:{2},{3}",param);
document.write(sb3.ToString());
</script>
</head>
<body>
</body>
</html>
StringBuilder.js 代码及示例下载: