關於字串操作 一

2022-02-11 13:50:51 字數 4491 閱讀 4495

在我們開發過程中跟介面打交道時,資料用的對多的格式應該就是字串了,因為介面空間上的值通常為字串型別,變數需要轉化為字串才能給各種空間複製,從控制項中讀取資料也是字串型別。很多時候字串就是其乙個中間轉化作用。下面詳細的介紹一些字串操作。

stringbuilder

類的部分屬性與方法

stringbuilder 類

string類的不可改變性使它更像乙個值型別而不是乙個引用型別。其***是每次執行字元操作時,都會建立乙個新的string物件。stringbuilder 類解決了對字串進行重複修改的過程中建立大量物件的問題。

stringbuilder類並沒有string 類的功能強大,只提供基本的替換和新增和刪除字串中的文字,但它的工作效率非常高,當定義stringbuilder物件時可以指定記憶體的記憶體容量,如果不指定系統就會根據物件初始化時的字串長度來確定。它有兩個主要引數length和capacity分別表示字串的實際長度和字串佔據的記憶體空間長度。對字串的修改就是在這個記憶體中進行的,大大提高了新增和替換的的效率

stringbuilder 類的部分屬性與方法

length 屬性

注意:它並不是唯讀的

stringbuilder sb=new stringbuilder("i love c#");

console.writeline(sb.tostring( )); 

sb.length = 6;

console.writeline(sb.tostring( )); //輸出 "i love"

capacity 屬性

描述:當前為例項分配的字元數量。預設容量是16,如果將乙個字串作為引數提供給建構函式,容量以最接近 2 的冪的值。

maxcapacity 屬性

描述:這個例項中可以被分配的字元最大數量。

描述:新增特定格式的字串

ensurecapacity( int capacity )  方法

描述:如果當前容量小於指定容量,記憶體分配會增加記憶體空間以達到指定容量。

insert()

描述:插入乙個子字串

remove()——

描述:從當前字串刪除字元

replace( char oldchar,char newchar )  方法

描述:用newchar替換oldchar。

replace( string oldstring,string newstring ) 方法

描述:用newstring替換oldstring。

replace( char oldchar,char newchar,int  startpos,int count ) 方法

描述:從startpos到count-1之間用newchar替換oldchar。

replace( string oldstring,string newstring,int startpos,int count ) 方法

描述:從startpos到count-1之間用newstring替換oldstring。

tostring( ) 方法

stringbuilder sb=new stringbuilder( "i live this game" );

string s1=sb.tostring( );         // "i live this game"

string s2=sb.tostring(3,4);     //displays "live"

在這裡第二個tostring( )方法呼叫了string類的substring( )方法

public string tostring( int startindex,int length )

string 類的部分屬性與方法

字元訪問

string s ="abcd";

console.writeline(s[0]); //

輸出"a"

console.writeline(s.length); //

輸出4

打散

string s ="abcd";

char arr =s.tochararray();//

打散成字元陣列

console.writeline(arr[0]);//

輸出"a"

擷取

string s ="abcd";

console.writeline(s.substring(1));//

從第二位一直擷取到最後,輸出

"bcd"

console.writeline(s.substring(2,2));//

從第三位開始擷取兩位,輸出

"bc"

字元匹配

string s ="abcd";

console.writeline(s.indexof('a')); //

從頭開始搜尋第乙個

a,輸出

"0"

大小寫轉換

string s ="abcd";

console.writeline(s.tolower());//

轉化為小寫,輸出

abcd

console.writeline(s.toupper());//

轉化為大寫,輸出

abcd

對齊

string s ="abcd";

console.writeline(s.padleft(6,'_'));//

使用'_'

填充字串左部,使它擴充到

6為長度

輸出''__abcd" 

console.writeline(s.padright(6,'_')) //

使用'_'

填充字串右部,使它擴充到

6為長度

輸出'' abcd__";

匹配移除

string s ="_a_b_c_d_";s.trim('_'));//

去掉前後的

'_'

輸出為"a_b_c_d"

s.trimstart('_'));//

去掉前面的

'_'

輸出為" a_b_c_d_"

console.writeline(

console.writeline(

console.writeline(s.trimend('_')); //

去掉後面的

'_'

輸出為" _ a_b_c_d"

插入和刪除

string s ="adef";

console.writeline(s.insert(1,"bc"));//

從第二位插入字串

bc 輸出

"abcdef"

console.writeline(s.remove(1));//將第2

位後面的刪除

輸出"a"

console.writeline(s.remove(0,2));//

將前兩位刪除

輸出"ef"

替換

string s="a_b_c_d";

console.writeline(s.replace('_',','));//

將字串中的

"_"替換為

","

輸出"a,b,c,d"

分割

string s=" a,b,c,d";

string arr=s.split(',');

關於字串相關操作

首先我們要建立乙個字串,並將字串複製給乙個變數。a i love you my lover a.find l start 0,end len a a.index l start 0,end len a 關於字串的切片操作。分為三個引數,開始 停止 步長。分別用冒號分割。例項 name jiangxi...

字串操作(一)

public class charactercompareemp public class searchlaststring else public class deletechar public static string removecharat string s,int location pu...

字串操作一

可以將字串的左右空格 t等空白內容去除,該函式可以將字串的左右兩邊 的空格 t等空白內容或指定字串去除,並返回處理後的結果,但原字串 並未被改變。不帶引數的strip 函式,表示把s中前後所有的空白字元全部去掉,包括 n t r 等不可見字串,可以理解為把s前後空白字 符串替換為none 帶引數的s...