Java中StringBuilder的清空方法比較

2021-09-08 10:45:21 字數 840 閱讀 8542

stringbuilder 沒有提供clear或empty方法。

清空有3種方法:

1)新生成乙個,舊的由系統自己主動**

2)使用delete

3)使用setlength

將三種方法迴圈1000萬次,**:

1.public class sbbm

20.          system.out.println( "way2="+(system.currenttimemillis()-time) );

21.          

22.23.        time = system.currenttimemillis();

24.        for( int i = 0; i < 10000000; i++ )

33.        system.out.println( "way1="+(system.currenttimemillis()-time) );

34.35.        time = system.currenttimemillis();

36.        for( int i = 0; i < 10000000; i++ )

46.        system.out.println( "way3="+(system.currenttimemillis()-time) );

47. 

48.     

49.    }

50.}

結果:1.way2=9438

2.way1=6281

3.way3=6469

能夠將各方法多反覆幾次,點到順序,等

整體來看:方法2好於方法3好於方法1

Java字串生成器StringBuilder

public class jerque long endtime system.currenttimemillis 定義對字串操作後的時間 long time endtime startime 計算對字串執行操作的時間 stringbuilder builder new stringbuilder ...

String一定差於StringBuilder嗎

string是固定不變的,而stringbuffer和stringbuilder可以改變它們的值。stringbuffer和stringbuilder唯一的區別是stringbuilder是同步的。當系統需要執行單執行緒時,使用stringbuilder比較好。stringbuilder比strin...

Java中equalsIgnoreCase的用法

equalsignorecase public boolean equalsignorecase string anotherstring 將此 string 與另乙個 string 進行比較,不考慮大小寫。如果兩個字串的長度相等,並且兩個字串中的相應字元都相等 忽略大小寫 則認為這兩個字串是相等的...