String 部分實用庫函式

2021-08-18 16:48:40 字數 1292 閱讀 7560

字串變數 string

用string建立字串的方法

①string s = new string(「a string」);

②string s =」a string」;

③scanner in=new scanner(system.in);

string s = in.nextline();    //使用者輸入s的值(字串)

system.out.println(s);

輸入 this is a line. 輸出:this is a line.

s = in.next(); //使用者輸入s的值(字串)next 用於讀入乙個詞,分界點是空格

輸入 this is a line. 輸出::this      

用equals判斷字串內容是否相等

system.out.println(s.equals(「bye」));

用compareto比較兩個字串誰大誰小

system.out.println(「abcd」.compareto(s1))

比較abcd字串與s1字串誰大誰小,當結果為1時,」abcd」更大;當結果為-1時,s1更大;當結果為0時,兩者相等。

用charat

訪問字串中對應位置的字元,從0開始計數

system.out.println(「abcd」.charat(0)) 輸出:a

system.out.println(「abcd」.charat(1)) 輸出:b

system.out.println(「abcd.」charat(2)) 輸出:c

system.out.println(「abcd.」charat(3)) 輸出:d

用substring

輸出從第x位之後的字元

system.out.println(「abcd」.substring(3)) 輸出:d

system.out.println(「abcd」.substring(2)) 輸出:cd

system.out.println(「abcd」.substring(1)) 輸出:bcd

用indexof輸出該字元在字串的第幾個位置上

system.out.println(「abcd」.indexof(b)) 輸出:1(從0開始計數)

system.out.println(「abcd」.indexof(bc)) 輸出:1(在第一號位置上)

用replace替換對應字元

system.out.println(「12345678」.replace(1,3)) 輸出:32345678

string部分庫函式的實現

1 模擬實現strcpy 思路分析 源字串的字元逐個賦值給目標字串 如下 include include char my strcpy char dest,const char src return ret int main my strcpy arr2,arr1 printf 拷貝完成後的字串 s...

實現部分庫函式

1.模擬實現strncat 與strcat無異,只是追加的塊大小不一樣,strncat只是向後追加n個位元組的內容 char my strncat char dst,const char src,int count while count 用數count控制迴圈的次數 dst src dst 0 r...

自己編寫的string庫函式

都是自己編寫的,僅供學習參考。date 2009.6.24 author summon function functions for string.h version v1.0 right all right opened char mystrcpy char pchdest,const char ...