字串函式 C

2021-08-10 07:54:30 字數 1088 閱讀 9877

c++ 中有大量的函式用來操作以 null 結尾的字串:

strcpy(s1, s2);

複製字串 s2 到字串 s1。

2strcat(s1, s2);

連線字串 s2 到字串 s1 的末尾。

3strlen(s1);

返回字串 s1 的長度。

4strcmp(s1, s2);

如果 s1 和 s2 是相同的,則返回 0;如果 s1s2 則返回值大於 0。

5strchr(s1, ch);

返回乙個指標,指向字串 s1 中字元 ch 的第一次出現的位置。

6strstr(s1, s2);

返回乙個指標,指向字串 s1 中字串 s2 的第一次出現的位置。

#include #include using namespace std;

int main ()

當上面的**被編譯和執行時,它會產生下列結果:

str3 

:hello

str1

+str2

:helloworld

str3

.size():

10

string類提供了一系列針對字串的操作,比如:

2. find() -- 在字串中查詢字串

4. insert() -- 插入字元

5. length() -- 返回字串的長度

6. replace() -- 替換字串

7. substr() -- 返回某個子字串

8. ...

下面是關於string類的例項:

#include

#include

using

namespace

std;

intmain

()

C 字串函式

c 字串函式 部分 方法 作用 compare 比較字串的內容,考慮文化背景 場所 確定某些字元是否相等 compareordinal 與compare 一樣,但不考慮文化背景 format 格式化包含各種值的字串和如何格式化每個值的說明符 indexof 定位字串中第一次出現某個給定子字串或字元的...

c 字串函式

2 strlen strcpy strcat strcmp strchr strcoll strstr strtok strtod strtol strcpy char strcpy char s1,const char s2 將字串 s2 複製到字串陣列 s1 中,返回 s1 的 值strcat ...

C 字串函式

功能 求取字串的長度 返回值 返回字串s1的長度 include using namespace std include intmain int argc,char ar 功能 複製字串 s2 到字串 s1。返回值 返回字串 s1 的起始位置 說明 如果字串 s1 的記憶體空間不夠大,可能會造成緩衝...