#include <string.h>
一、str 系列 1.strtok extern char *strtok( char *s, const char *delim ); 功能:分解字符串為一組標(biāo)記串。s為要分解的字符串,delim為分隔符字符串。 說明:strtok()用來將字符串分割成一個個片段。當(dāng)strtok()在參數(shù)s的字符串中發(fā)現(xiàn)到參數(shù)delim的分割字符時則會將該字符改為 \0 字符。在第一次調(diào)用時,strtok()必需給予參數(shù)s字符串,往后的調(diào)用則將參數(shù)s設(shè)置成NULL。每次調(diào)用成功則返回被分割出片段的指針。當(dāng)沒有被分割的串時則返回NULL。所有delim中包含的字符都會被濾掉,并將被濾掉的地方設(shè)為一處分割的節(jié)點(diǎn)。 舉例: /* strtok example */ #include <stdio.h> #include <string.h> int main (void) { 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, " ,.-"); } printf("at the end: %s", str); return 0; } Splitting string "- This, a sample string." into tokens: This a sample string the end: - This 注:strtok函數(shù)會破壞被分解字符串的完整,調(diào)用前和調(diào)用后的s已經(jīng)不一樣了。另外貌似制表符 \t 無法充當(dāng)分割字符。 2.strstr char * strstr( const char * str1, const char * str2 ); 功能:從字符串 str1 中尋找 str2 第一次出現(xiàn)的位置(不比較結(jié)束符NULL),如果沒找到則返回NULL。 舉例: /* strstr example */ #include <stdio.h> #include <string.h> int main () { char str[] = "This is a simple string"; char *pch; pch = strstr(str, "simple"); strncpy(pch, "sample", 6); puts(pch); puts(str); return 0; } sample string This is a sample string 3.strchr char * strchr ( const char *str, int ch ); 功能:查找字符串 str 中首次出現(xiàn)字符 ch 的位置 舉例: /* strchr example */ #include <stdio.h> #include <string.h> int main () { char str[] = "This is a simple string"; char *pch; printf("Looking for the 's' character in \"%s\"...\n", str); pch = strchr(str, 's'); while (pch != NULL){ printf("found at %d th\n", pch - str + 1); pch = strchr(pch + 1, 's'); } return 0; } Looking for the 's' character in "This is a simple string"... found at 4 th found at 7 th found at 11 th found at 18 th 4.strcpy char * strcpy( char * dest, const char * src ); 功能:把 src 所指由NULL結(jié)束的字符串復(fù)制到 dest 所指的數(shù)組中。 類似的: strncpy char * strncpy( char * dest, const char * src, size_t num ); stpcpy 非庫函數(shù),用法跟 strcpy 完全一樣 5.strcat char * strcat ( char * dest, const char * src ); 功能:把 src 所指字符串添加到 dest 結(jié)尾處(覆蓋dest結(jié)尾處的'\0')并添加'\0'。 類似的 strncat char * strncat ( char * dest, const char * src, size_t num ); 6.strcmp int strcmp ( const char * str1, const char * str2 ); 功能:比較字符串 str1 和 str2。 類似的: strncmp int strncmp ( const char * str1, const char * str2, size_t num ); strcasecmp extern int strcasecmp(const char *str1, const char *str2); strncasecmp extern int strncasecmp(const char *str1, const char *str2, size_t num); 7.strcspn size_t strcspn ( const char * str1, const char * str2 ); 功能:在字符串 s1 中搜尋 s2 中所出現(xiàn)的任一個字符。 /* strcspn example */ #include <stdio.h> #include <string.h> int main () { char str[] = "fcba73"; char keys[] = "1234567890"; int i = strcspn (str, keys); printf ("Already read %d characters\n", i); printf ("The first number in both str and keys is %d th\n", i + 1); return 0; } 類似的 strspn (Returns the length of the initial portion of str1 which consists only of characters that are part of str2.) size_t strspn ( const char * str1, const char * str2 ); #include <stdio.h> #include <string.h> int main () { char str[] = "1589fcba73"; char keys[] = "1234567890"; int i = strspn (str, keys); printf ("the beginning %d characters are all in keys \n", i); return 0; } the beginning 4 characters are all in keys 8.strlen size_t strlen ( const char * str ); 功能:計算字符串 str 的長度 類似的 strnlen size_t strnlen(const char *str, size_t maxlen); 9.strdup extern char *strdup( char *str ); 功能:復(fù)制字符串 str #include <stdio.h> #include <string.h> int main () { char *str = "1234567890"; char *p = strdup(str); printf("the duplicated string is: %s\n", p); return 0; }
二、mem 系列 1.memset void * memset ( void * ptr, int value, size_t num ); 功能:把 ptr 所指內(nèi)存區(qū)域的前 num 個字節(jié)設(shè)置成字符 value。 舉例: #include <stdio.h> #include <string.h> int main () { char str[] = "almost erery programer should know memset!"; memset(str, '-', sizeof(str)); printf("the str is: %s now\n", str); return 0; } 2.memmove void * memmove ( void * dest, const void * src, size_t num ); 功能:由 src 所指內(nèi)存區(qū)域復(fù)制 num 個字節(jié)到 dest 所指內(nèi)存區(qū)域。 舉例: #include <stdio.h> #include <string.h> int main () { char str[] = "memmove can be very useful......"; memmove(str + 20, str + 15, 11); printf("the str is: %s\n", str); return 0; } the str is: memmove can be very very useful.3.memcpy void * memcpy ( void * destination, const void * source, size_t num ); 類似 strncpy。區(qū)別:拷貝指定大小的內(nèi)存數(shù)據(jù),而不管內(nèi)容(不限于字符串)。 4.memcmp int memcmp ( const void * ptr1, const void * ptr2, size_t num ); 類似 strncmp 5.memchr void * memchr ( const void *buf, int ch, size_t count); 功能:從 buf 所指內(nèi)存區(qū)域的前 count 個字節(jié)查找字符 ch。
|
|
|