StringBuilder類中的重要方法

2021-08-08 20:04:10 字數 2172 閱讀 8456

下面的api註解包含了stringbuilder類中的重要方法

capacity():返回當前容量。

charat(int index):返回此序列中指定索引處的 char 值。

codepointat(int index):返回指定索引處的字元(統一**點)。

codepointbefore(int index):返回指定索引前的字元(統一**點)。

codepointcount(int beginindex,int endindex):返回此序列指定文字範圍內的統一**點。

delete(int start,int end):移除此序列的子字串中的字元。

deletecharat(int index):移除此序列指定位置上的 char。

ensurecapacity(int minimumcapacity):確保容量至少等於指定的最小值。

getchars(int srcbegin,int srcend,char dst,int dstbegin):將字元從此序列複製到目標字元陣列 dst。

indexof(string str):返回第一次出現的指定子字串在該字串中的索引。

indexof(string str,int fromindex):從指定的索引處開始,返回第一次出現的指定子字串在該字串中的索引。

insert(int offset,boolean b):將 boolean 引數的字串表示形式插入此序列中。

insert(int offset,char c):將 char 引數的字串表示形式插入此序列中。

insert(int offset,char str):將 char 陣列引數的字串表示形式插入此序列中。

insert(int index,char str,int offset,int len):將陣列引數 str 子陣列的字串表示形式插入此序列中。

insert(int dstoffset,charsequence s):將指定 charsequence 插入此序列中。

insert(int dstoffset,charsequence s,int start,int end):將指定 charsequence 的子串行插入此序列中。

insert(int offset,double d):將 double 引數的字串表示形式插入此序列中。

insert(int offset,float f):將 float 引數的字串表示形式插入此序列中。

insert(int offset,int i):將 int 引數的字串表示形式插入此序列中。

insert(int offset,long l):將 long 引數的字串表示形式插入此序列中。

insert(int offset,object obj):將 object 引數的字串表示形式插入此字串行中。

insert(int offset,string str):將字串插入此字串行中。

lastindexof(string str):返回最右邊出現的指定子字串在此字串中的索引。

lastindexof(string str,int fromindex):返回最後一次出現的指定子字串在此字串中的索引。

length():返回長度(字元數)。

offsetbycodepoints(int index,int codepointoffset):返回此序列中的乙個索引,該索引是從給定 index 偏移 codepointoffset 個**點後得到的。

replace(int start,int end,string str):使用給定 string 中的字元替換此序列的子字串中的字元。

reverse():將此字串行用其反轉形式取代。

setcharat(int index,char ch):將給定索引處的字元設定為 ch。

setlength(int newlength):設定字串行的長度。

subsequence(int start,int end):返回乙個新字串行,該字串行是此序列的子串行。

substring(int start):返回乙個新的 string,它包含此字串行當前所包含字元的子串行。

substring(int start,int end):返回乙個新的 string,它包含此序列當前所包含字元的子串行。

tostring():返回此序列中資料的字串表示形式。

trimtosize():嘗試減少用於字串行的儲存空間。

C 中 StringBuilder類使用

c 型別分為值型別 分配空間為棧,存放值 和引用型別 分配空間為堆,存放指標 string型別為引用型別。string物件,一旦賦值就不可改變 叫做字串的恆定性 賦值完了之後,再對string進行拼接,賦值等,都會再記憶體裡面重新分配乙個新的記憶體空間。在需要對字串執行重複修改的情況下,與建立新的 ...

C 中StringBuilder類的使用

string 物件是不可改變的。每次使用 system.string 類中的方法之一時,都要在記憶體中建立乙個新的字串物件,這就需要為該新物件分配新的空間。在需要對字串執行重複修改的情況下,與建立新的 string 物件相關的系統開銷可能會非常昂貴。如果要修改字串而不建立新的物件,則可以使用 sys...

C 中StringBuilder類的使用

通過用乙個過載的建構函式方法初始化變數,可以建立 stringbuilder 類的新例項,正如以下示例中所闡釋的那樣。stringbuilder mystringbuilder new stringbuilder hello world 一 設定容量和長度 雖然 stringbuilder 物件是動...