JAVA字串處理函式

2021-06-10 03:05:11 字數 1684 閱讀 1799

1、substring() 擷取字串

第一種是:string substring(int startindex)

第二種是:string substring(int startindex,int endindex)

2、concat() 連線兩個字串

3、replace() 替換

第一種形式用乙個字元在呼叫字串中所有出現某個字元的地方進行替換

string replace(char newchar,char oldchar);

例如:string s=」hello」.replace(』l',』w');

第二種形式是用乙個字串行替換另乙個字串行

string replace(charsequence original,charsequence replacement);

4、trim() 去掉起始和結尾的空格

5、valueof() 轉換為字串

6、tolowercase() 轉換為小寫

7、touppercase() 轉換為大寫

8、length() 取得字串的長度

9、charat() 擷取乙個字元

例:char ch;

ch=」abc」.charat(1);

返回值為』b』

10、getchars() 擷取多個字元

void getchars(int sourcestart,int sourceend,char target,int targetstart)

sourcestart 指定了子串開始字元的下標

sourceend 指定了子串結束後的下乙個字元的下標。因此,子串包含從sourcestart到sourceend-1的字元。

target 指定接收字元的陣列

targetstart target中開始複製子串的下標值

例:string s=」this is a demo of the getchars method.」;

char buf=new char[20];

s.getchars(10,14,buf,0);

11、getbytes()

替代getchars()的一種方法是將字元儲存在位元組陣列中,該方法即getbytes()

例:string s = 「hello!你好!」;

byte bytes = s.getbytes();

12、tochararray()將乙個字串轉換成乙個字元陣列

例:string s = 「hello!你好!」;

char ss = s.tochararray();

13、equals()和equalsignorecase() 比較兩個字串

14、regionmatches() 用於比較乙個字串中特定區域與另一特定區域,它有乙個過載的形式允許在比較中忽略大小寫

boolean regionmatches(int startindex,string str2,int str2startindex,int numchars)

boolean regionmatches(boolean ignorecase,int startindex,string str2,int str2startindex,int numchars)

15、startswith()和endswith()

startswith()方法決定是否以特定字串開始,endwith()方法決定是否以特定字串結束

Java字串處理函式

substring 它有兩種形式,第一種是 string substring int startindex 第二種是 string substring int startindex,int endindex concat 連線兩個字串 replace 替換它有兩種形式,第一種形式用乙個字元在呼叫字串...

字串處理函式

1 puts 向顯示器輸出字串 原型 int puts const char s 標頭檔案 include 返回值 成功返回輸出的字元數,失敗返回eof puts 函式與printf 輸出字串的區別 1.puts在輸出字串時,遇到 0 會自動終止輸出,並將 0 轉換為 n 來輸出 2.printf在...

字串處理函式

puts 函式 用來向標準輸出裝置 螢幕 寫字串並換行,其呼叫格式為 puts s 其中s為字串變數 字串陣列名或字串指標 puts 函式的作用與語printf s n s 相同,將緩衝區的字元輸出到標準輸出,遇到空字元截至,並且在末尾新增乙個換行符。gets 函式用來從標準輸入裝置 鍵盤 讀取字串...