頭文件:string.h
1.strchr
const char * strchr ( const char * str, int character );
Locate first occurrence of character in string
str
C string.
character
Character to be located.
Return Value
A pointer to the first occurrence of? character? in? str .
If the?character?is not found, the function returns a null pointer.
2.strlen
function
<cstring>
strlen
size_t strlen ( const char * str );
Get string length
Return Value
Returns the length of the C string?str.
3.strcmp
char * strcpy ( char * destination, const char * source );
Copy string
Parameters
destination
Pointer to the destination array where the content is to be copied.
source
C string to be copied.
Return Value
destination ?is returned.
4.strcat
char * strcat ( char * destination, const char * source );
Concatenate strings? ? //追加字符串
Parameters
destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
source
C string to be appended. This should not overlap?destination.
Return Value
destination ?is returned.
注:
源字符串必須以 '\0' 結束。
目標空間必須有足夠的大,能容納下源字符串的內容。
目標空間必須可修改。
5.strcmp
int strcmp ( const char * str1, const char * str2 );
Compare two strings
Parameters
str1
C string to be compared.
str2
C string to be compared.
Return Value
Returns an integral value indicating the relationship between the strings:
return value | indicates |
<0 | the first character that does not match has a lower value in? ptr1?than in? ptr2 |
0 | the contents of both strings are equal |
>0 | the first character that does not match has a greater value in? ptr1?than in? ptr2 |
6.strncpy? :長度限制為num字節
char * strncpy ( char * destination, const char * source, size_t num );
Copy characters from string
7.strncat? :追加長度為n字節
char * strncat ( char * destination , const char * source , size_t num );
8.strncmp? :比較個數為n字節
9.strstr :查找子串
char * strstr ( const char * str1 , const char * str2 );
Returns a pointer to the first occurrence of str2 !!!in str1!!!, or a null pointer if str2 is not part of str1.
注意:返回的是在str1中的指針
10.strtok:截斷字符串、存在記憶功能
char * strtok ( char * str , const char * sep );
1.sep 參數是個字符串,定義了用作分隔符的字符集合
2.第一個參數指定一個字符串,它包含了 0 個或者多個由 sep 字符串中一個或者多個分隔符分割的標 記。
3. strtok 函數找到 str 中的下一個標記,并將其用 \0 結尾,返回一個指向這個標記的指針。(注:
strtok 函數會改變被操作的字符串,所以在使用 strtok 函數切分的字符串一般都是? ?!!! 臨時拷貝 !!!? ?的內容 并且可修改。)
4. strtok 函數的第一個參數不為 NULL ,函數將找到 str 中第一個標記, strtok 函數將保存它在字符串 中的位置,作為一個標記。
5 . strtok 函數的第一個參數為 NULL ,函數將在同一個字符串中被保存的位置開始,查找下一個標 記。
11.strerror
char * strerror ( int errnum );??
返回錯誤碼所對應的錯誤信息。(只是返回,不打印)
常結合errno這個全局變量使用(頭文件為errno.h)
errno記錄著錯誤碼
12.perror:打印錯誤碼對應的錯誤信息
#include <stdio.h>
#include <errno.h>
int main() {
FILE* file = fopen( "none.txt", "r");
if (file == NULL) {
perror( "Error");?? //打印為:Error: No such file or directory?????? //自動換行
perror( "Error fopen");?? //打印為:Error fopen: No such file or directory
return 1;
}
return 0;
}