java中concat()方法的使用說(shuō)明
concat()方法介紹:
將幾個(gè)字符串連接到一起。
例如:
s = s.concat(str1);//將字符串str1接到字符串s后面
s = s.concat(str2);//將字符串str1接到字符串s后面
代碼:
public class Test { public static void main(String[] args){ String s = '厲害了,'; String str1 = '我的'; String str2 = '國(guó)!'; s = s.concat(str1);//將字符串str1接到字符串s后面 s = s.concat(str2);//將字符串str1接到字符串s后面 System.out.println(s); } }
運(yùn)行結(jié)果:
厲害了,我的國(guó)!
補(bǔ)充知識(shí):Java| String 字符串拼接方法 concat 和 + 效率比較
測(cè)試代碼:
public static void main(String[] args) { String str1 = 'yveshe'; String str2 = 'hello'; /** * concat */ System.gc(); long startTime1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { str1 = str1.concat(str2); } long endTime1 = System.currentTimeMillis(); System.out.println('concat:' + (endTime1 - startTime1)); /** * + */ str1 = 'yveshe'; System.gc(); long startTime2 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { str1 = str1 + str2; } long endTime2 = System.currentTimeMillis(); System.out.println('+: ' + (endTime2 - startTime2));}
測(cè)試結(jié)果:
concat:231
+: 468
總結(jié):
1.concat的計(jì)算效率要比+的效率高
2.concat只適用于string和string的拼接,+適用于string和任何對(duì)象的拼接
3.當(dāng)在少量的數(shù)據(jù)拼接時(shí),使用concat和+都行,如果是大量的數(shù)據(jù)拼接,建議使用StringBuilder或者StringBuffer.
以上這篇java中concat()方法的使用說(shuō)明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python web框架的總結(jié)2. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式3. Python如何進(jìn)行時(shí)間處理4. python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)5. 詳解Python模塊化編程與裝飾器6. Python基于pyjnius庫(kù)實(shí)現(xiàn)訪問(wèn)java類(lèi)7. Python使用shutil模塊實(shí)現(xiàn)文件拷貝8. Python實(shí)現(xiàn)迪杰斯特拉算法過(guò)程解析9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python裝飾器三種裝飾模式的簡(jiǎn)單分析
