字符串函數總結
1.strlen
size_t strlen ( const char * str );
字符串已經 ‘\0’ 作爲結束標誌,strlen函數返回的是在字符串中 ‘\0’ 前面出現的字符個數(不包含 ‘\0’ )。 參數指向的字符串必須要以 ‘\0’ 結束。 注意函數的返回值爲size_t,是無符號的(易錯)。
2.strcpy
char* strcpy(char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
源字符串必須以 ‘\0’ 結束。 會將源字符串中的 ‘\0’ 拷貝到目標空間。 目標空間必須足夠大,以確保能存放源字符串。 目標空間必須可變。
3.strcat
char * strcat ( char * destination, const char * source );
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
源字符串必須以 ‘\0’ 結束。 目標空間必須有足夠的大,能容納下源字符串的內容。 目標空間必須可修改。
4.strcmp
int strcmp ( const char * str1, const char * str2 );
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
標準規定: 第一個字符串大於第二個字符串,則返回大於0的數字 第一個字符串等於第二個字符串,則返回0 第一個字符串小於第二個字符串,則返回小於0的數字 .注意:一般爲if(strcmp ( str1, str2 )==0)
5.strncpy
char * strncpy ( char * destination, const char * source, size_t num )
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
拷貝num個字符從源字符串到目標空間。 如果源字符串的長度小於num,則拷貝完源字符串之後,在目標的後邊追加0,直到num個。
注意一般取size_t num爲strlen(str2)+1
6.strncat
char * strncat ( char * destination, const char * source, size_t num )
Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating nullcharacter is copied.
注意一般取size_t num爲strlen(str2)+1
7.strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
比較到出現另個字符不一樣或者一個字符串結束或者num個字符全部比較完。
8.strstr
char * strstr ( const char *, const char * );
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
如果找到從找到位置返回
9.strtok
char * strtok ( char * str, const char * sep );
sep參數是個字符串,定義了用作分隔符的字符集合
第一個參數指定一個字符串,它包含了0個或者多個由sep字符串中一個或者多個分隔符分割的標記。
strtok函數找到str中的下一個標記,並將其用 \0 結尾,返回一個指向這個標記的指針。(注:strtok函數會改 變被操作的字符串,所以在使用strtok函數切分的字符串一般都是臨時拷貝的內容並且可修改。)
strtok函數的第一個參數不爲 NULL ,函數將找到str中第一個標記,strtok函數將保存它在字符串中的位置。
strtok函數的第一個參數爲 NULL ,函數將在同一個字符串中被保存的位置開始,查找下一個標記。 如果字符串中不存在更多的標記,則返回 NULL 指針
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
#include <stdio.h>
int main()
{
char *p = "[email protected]";
const char* sep = ".@";
char arr[30];
char *str = NULL;
strcpy(arr, p);//將數據拷貝一份,處理arr數組的內容
for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep))
{
printf("%s\n", str);
}
}
10.strerror
char * strerror ( int errnum );
返回錯誤碼,所對應的錯誤信息。
/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>
//必須包含的頭文件
int main ()
{
FILE * pFile;
pFile = fopen ("unexist.ent","r");
if (pFile == NULL)
printf ("Error opening file unexist.ent: %s\n",strerror(errno)); //errno: Last error number
return 0;
}