string與stringbuffer的區別
簡單地說,就是乙個變數和常量的關係.stringbuffer物件的內容可以修改;而字串物件一旦產生後就不可以被修改,重新賦值其實是兩個物件
stringbuffer內部實現方式和字串不同
stringbuffer的在進行字串處理時,不生成新的物件,在記憶體使用上要優於串類。所以在實際使用時,如果經常需要對乙個字串進行修改,例如插入,刪除等操作,使用stringbuffer要更加適合一些。
舉乙個簡單的例子:
上邊的**的效率很高,因為只建立了乙個stringbuffer物件,而下邊的**的效率很低,因為建立了101個物件
string str=newstring();而在某些特別情況下, string 物件的字串拼接其實是被 jvm 解釋成了 stringbuffer 物件的拼接,所以這些時候 string 物件的速度並不會比 stringbuffer 物件慢,而特別是以下的字串物件生成中, string 效率是遠要比 stringbuffer 快的:for(int i=0;i<100;i++)
string s1 =
"this is only a"
+" ******"
+" test"
;
你會很驚訝的發現,生成 string s1 物件的速度簡直太快了,而這個時候 stringbuffer 居然速度上根本一點都不佔優勢。其實這是 jvm 的乙個把戲,在 jvm 眼裡,這個stringbuffer sb=
newstringbuilder
("this is only a").
(" ******").
(" test"
);
其實就是:string s1 =
"this is only a"
+" ******"
+"test"
;
string s1 =
"this is only a ****** test"
;
這時候 jvm 會規規矩矩的按照原來的方式去做string s2 =
"this is only a"
;string s3 =
" ******"
;string s4 =
" test"
;string s1 = s2 +s3 + s4;
String與string的區別
1 string是乙個類,string是一種資料型別.2 string是c 中的類,string是.net framework的類 在c ide中不會顯示藍色 3 c string對映為.net framework的string 4 如果用string,編譯器會把它編譯成string,所以如果直接用...
string與string標頭檔案
先來段 我是在vs2012上實驗的 include stdafx.h include include include using namespace std int tmain int argc,tchar argv cout endl for rit mapstudent.rbegin rit m...
String與string的區別
c 是區分大小寫的,但是我卻發現c 中同時存在string與string,於是我很困惑,於是我上網搜尋了一下,於是我了解了一些小知識。msdn中對string的說明 stringis analiasforstringin the net framework。string是string的別名而已,st...