string
text="我是攻城師1";
string text2=new
string("我是攻城師2")
+號與concat方法的: 首先+號是可以和各種型別拼接字串的,而concat的引數必須是字串型別,其次concat方法拼接null值是會拋空指標的,**如下:
//difference 1
string a = null;
string b = "foo";
string c = a + b; // c = nullfoo
string d = b.concat(a); // 空指標
string e = a.concat(b); // 空指標
//difference 2
string x = "1" + 2; // x = 12
string y = "1".concat(2); // 編譯錯誤
string c ="a" + "b";
c = new stringbuilder()
.tostring();
string c=」a「.concat("b")
public
string
concat(string
str)
int len = value.length;
char buf = arrays.copyof(value, len + otherlen);
str.getchars(buf, len);
return
newstring(buf, true);
}
final
int loop_count=10000;
// 1 +
string s1="s1";
long starttime=system.nanotime();
for (int i = 0; i < loop_count; i++)
long duration=system.nanotime()-starttime;
system.out.println(" + cost: "+duration/1000+" 微秒 ");
// 2 concat
string s2="s2";
starttime=system.nanotime();
for (int i = 0; i < loop_count; i++)
duration=system.nanotime()-starttime;
system.out.println(" concat cost: "+duration/1000+" 微秒 ");
// 3 stringbuffer
stringbuffer s3=new stringbuffer("s3");
starttime=system.nanotime();
for (int i = 0; i < loop_count; i++)
duration=system.nanotime()-starttime;
system.out.println(" stringbuffer cost: "+duration/1000+" 微秒 ");
// 4 stringbuilder
stringbuilder s4=new stringbuilder("s4");
starttime=system.nanotime();
for (int i = 0; i < loop_count; i++)
duration=system.nanotime()-starttime;
system.out.println(" stringbuilder cost: "+duration/1000+" 微秒 ");
+ cos
t:432673
微秒 concat cos
t:49118
微秒 stringbuffer cos
t:1011
微秒 stringbuilder cos
t:734
微秒
js裡面的字串擷取
substr 方法可在字串中抽取從 start 下標開始的指定數目的字元。使用 substr start num 栗子1 var str sxswnspcx var result str.substr 3 start,num console.log result 得到 wnspcx 栗子2 var ...
Java 字串拼接效率比較
package com.jtzen9 public class main system.out.println system.currenttimemillis time string str2 a time system.currenttimemillis for int i 0 i 50000 ...
拼接字串
border 1 class box 標籤名稱th 是否顯示th 標籤順序th tr thead 首頁td class check 是option 否option select td class number 1option 2option 3option 4option 5option 6opti...