• C 字符串操作函数集


    http://blog.minidx.com/2008/02/03/469.html

    http://nengxia.com/clib/string/strchr.html  字符串函数集合描述

    C语言不像Java,Php之类的高级语言,对象中直接封装了字符串的处理函数。C语言中进行普通的字符串处理也经常会让我们焦头烂额……不过好在C语言中还是提供了像strtok这样功能强大的字符串处理函数,可以帮我们实现部分需要的功能。下面我们介绍一下strtok函数的用法以及具体的使用例。

    原型:char *strtok(char *s, char *delim);
    功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
    说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
    返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

    具体使用例子:

    Download: strtok_sample.c

    1. #include <stdio.h>
    2. #include <string.h>
    3. int main(int argc,char **argv)
    4. {
    5. char buf1[]="aaa, ,a, ,,,bbb-c,,,ee|abc";
    6. /* Establish string and get the first token: */
    7. char* token = strtok( buf1, ",-|");
    8. while( token != NULL )
    9. {
    10. /* While there are tokens in "string" */
    11. printf( "%s ", token );
    12. /* Get next token: */
    13. token = strtok( NULL, ",-|");
    14. }
    15. return 0;
    16. }

    问题:

    char * buf1=”aaa, ,a, ,,,bbb-c,,,ee|abc”;
    buff1指向一个常量区。
    根据
    If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.注意这句话中有overwirtes这个单词,常量区是不能overwirte的!!!,所以得用char buff1[]。

    =======

      原型:extern char *strchr(char *s,char c);
           
      用法:#include <string.h>
     
      功能:查找字符串s中首次出现字符c的位置
     
      说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
     
      举例:


          // strchr.c
         
          #include <syslib.h>
          #include <string.h>

          main()
          {
            char *s="Golden Global View";
            char *p;
           
            clrscr();
           
            strchr(s,'V');
            if(p)
              printf("%s",p);
            else
              printf("Not Found!");

            getchar();
            return 0;
          }
    =======

      原型:extern char *strstr(char *haystack, char *needle);
           
      用法:#include <string.h>
     
      功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。
     
      说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
     
      举例:


          // strstr.c
         
          #include <syslib.h>
          #include <string.h>

          main()
          {
            char *s="Golden Global View";
            char *l="lob";
            char *p;
           
            clrscr();
           
            p=strstr(s,l);
            if(p)
              printf("%s",p);
            else
              printf("Not Found!");

            getchar();
            return 0;
          }

     =====

      原型:extern char *strcat(char *dest,char *src);
           
      用法:#include <string.h>
     
      功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
     
      说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
            返回指向dest的指针。

  • 相关阅读:
    CSS盒子模型
    getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
    MYSQL中的CASE WHEN END AS
    单点登录的精华总结
    git&github
    June 21st 2017 Week 25th Wednesday
    June 20th 2017 Week 25th Tuesday
    June 19th 2017 Week 25th Monday
    June 18th 2017 Week 25th Sunday
    June 17th 2017 Week 24th Saturday
  • 原文地址:https://www.cnblogs.com/leaven/p/1923302.html
Copyright © 2020-2023  润新知