C语言常用的各种操作字符串的函数

  • /*字符串长度函数*/
  •   size_t strlen(const char *string); 

  • /*不受限制的字符串函数*/ 
  •   char *strcpy(char *dst, const char *src); 
  •   char *strcat(char *dst, const char *src); 
  •   int strcmp(const char *s1, const char *s2); 

  • /*长度受限的字符串函数*/ 
  •   char *strncpy(char *dst, const char *src, size_t len); 
  •   char *strncat(char *dst, const char *src, size_t len); 
  •   int strncmp(const char *s1, const char *s2, size_t len); 

  • /*字符串查找*/ 
  •     /*
  •   * 功能:查找字符串s中首次出现字符c的位置
  •   * 说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
  •     */
  •   char *strchr(const char *str, int ch); 
  •     
  •    //功能:查找字符串s中最后一次出现字符c的位置
      * 说明:返回最后一次出现c的位置的指针,如果s中不存在c则返回NULL。
  •   char *strrchr(const char *str, int ch); 
  •  
  •     
  • /*
  •   * 功能:依次检验字符串str中的字符,当被检验字符在字符串group中也包含时,则停止检验,并返回该字     * 符位置,空字符NULL不包括在内。
  •   * 说明:返回str中第一个满足条件的字符的指针,如果没有匹配字符则返回空指针NULL。
  •   */
  •     char *strpbrk(const char *str, const char *group); 

  •     /*
  •   * 功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。
  •   * 返回值:返回该位置的指针,如找不到,返回空指针。
  •   */
  •     char *strstr(const char *str1, const char *str2); 
  •     
  •     

  •  /*
  •      * 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
  •      * 说明:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串      * ,当strtok()
  •      * 在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为\0 字符。在第一次调用时,strtok(      * )必需给予参数
  •      * s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。
  •      * 返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都      * 会被滤掉,并将
  •      * 被滤掉的地方设为一处分割的节点。
  •      */
  •      char *strtok(char *s, const char *delim);
  •     


  • /*第一次出现的起始位置*/ 
  •     /*
  •      * 函数说明 strspn()从参数str 字符串的开头计算连续的字符,而这些字符都完全是group 所指字符串中      * 的字符。简单的说,
  •      * 若strspn()返回的数值为n,则代表字符串str 开头连续有n 个字符都是属于字符串group内的字符。
  •    * 返回值 返回字符串str开头连续包含字符串group内的字符数目。
  •   */
  •     size_t strspn(const char *str, const char *group); 
  •     
  •    

  •  /*
  •     * 功能:顺序在字符串str中搜寻与group中字符的第一个相同字符,返回这个字符在Str中第一次出现的位置
  •   * 说明:(返回第一个出现的字符在str中的下标值,亦即在str中出现而group中没有出现的子串的长度。)
  •   * 简单地说,若strcspn()返回的数值为n,则代表字符串str开头连续有n个字符都不含字符串group内的字符
  •   */
  •      size_t strcspn(const char *str, const char *group); 

  • /*大小写字符转换函数*/ 
  •   int tolower(int ch); 
  •   int toupper(int ch); 
  • /*内存操作函数*/ 
  •   void *memcpy(void *dst, const void *src, size_t length); 
  •   void *memmove(void *dst, const void *src, size_t length); 
  •   void *memcmp(const void *a, const void *b, size_t length); 
  •   void *memset(void *a, int ch, size_t length);
  • strtok函数
  • 原型:char *strtok(char *s, char *delim);

    功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。

    说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。 strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。 返回指向下一个标记串。当没有标记串时则返回空字符NULL。

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. int main(int argc, char * argv[])  
    5. {  
    6.     //时间格式 2010/08/11 10:38:22  
    7.     char strEventTime[] = "2010/08/11 10:38:22";  
    8.     char *token = NULL;  
    9.       
    10.     token = strtok(strEventTime, "/");  
    11.     char *year = token;  
    12.     if (token != NULL)  
    13.     {  
    14.         token = strtok(NULL, "/");  
    15.     }  
    16.     char *month = token;  
    17.     if (token != NULL)  
    18.     {  
    19.         token = strtok(NULL, " ");  
    20.     }  
    21.     char *day = token;  
    22.     if (token != NULL)  
    23.     {  
    24.         token = strtok(NULL, ":");  
    25.     }  
    26.     char *hour = token;  
    27.     if (token != NULL)  
    28.     {  
    29.         token = strtok(NULL, ":");  
    30.     }  
    31.     char *minute = token;  
    32.   
    33.     if (token != NULL)  
    34.     {  
    35.         token = strtok(NULL, ":");  
    36.     }  
    37.     char *second = token;  
    38.   
    39.     printf("%s %s %s %s %s %s %s\n", year, month, day, hour, minute, second);  
    40.     return 0;  
    41. }  
    strdup函数
  • 原型:char *strdup(const char *s);
  • The strdup function returns a pointer to a new string which is a duplicate of the string s.Memory for the new string is obtained with malloc,and can be freeed with free.

  • strcspn函数
  • 原型:size_t strcspn(const char *s,const char *reject);
  • this function calculates the length of the initial segment of 's' which consists entirely of characters not in 'reject'.
  • returns the number of characters in the initial segment of s which are not in the strings 'reject' 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值