zoukankan      html  css  js  c++  java
  • C语言字符串处理

    字符串和字符串数组声明和初始化

        char buff[1024] = {''};
        char *str1;

    字符串和字符串数组间的转化

        
    /* 字符数组可以直接转字符串,但字符串不能直接转字符数组 */
    char buff[1024] = "successing to read the file!"; char *str1; str1 = buff;

    字符串和字符串数组长度量取

    size_t strlen(char const *str)
        char buff[1024] = "successing to read the file!";
        char *str1;
        int len = 0;
        str1 = buff;
        len = strlen(str1);

    复制字符串

    char *strcpy(char const *cpystr, char const *str)
        char buff[1024] = "successing to read the file!";
        char *str1,*cpystr;
        int len = 0;
        str1 = buff;
        strcpy(cpystr, str1);

    连接字符串

     char *strcat(char const *str1, char const *str2)
        char buff[1024] = "successing to read the file!";
        char *str1,*p;
        char *chs = "to";
        str1 = buff;
        p = strcat(str1, chs);
    
        printf("str1 = %s
    ", str1);
        printf("chs = %s
    ", chs);
        printf("p = %s
    ", p);

     比较字符串

    char *strcmp(char const *str1, char const *str2)
        char buff[1024] = "successing to read the file!";
        char *str1,*str2;
        char *chs = "successing";
        int num = 0;
        str1 = buff;
        strcpy(str1, str2);
    
        num = strcmp(str1, chs);
        printf("num1 = %d
    ", num);
        num = strcmp(str1, str2);
        printf("num2 = %d
    ", num);
        num = strcmp(chs, str1);
        printf("num3 = %d
    ", num);

    在一个字符串中查找一个字符

    /* 从左往右查找,第一次出现 */
    char
    *strchr(char const *str, int ch)
    /* 从右往左查找,第一次出现 */
    char
    *strchr(char const *str, int ch)
        char buff[1024] = "successing to read the file!";
        char *str1,*lp,*rp;
        char ch = 'c';
        str1 = buff;
        lp = strchr(str1, ch);
        rp = strrchr(str1, ch);

    在一个字符串中查找任意几个个字符

    在一个字符串中查找一个子字符串

    char *strstr(char const *str, char const *chs)
        char buff[1024] = "successing to read the file!";
        char *str1,*p;
        char *chs = "to";
        str1 = buff;
        p = strstr(str1, chs);
    
        printf("str1 = %s
    ", str1);
        printf("p = %s
    ", p);

  • 相关阅读:
    【转】 mysql反引号的使用(防冲突)
    【百度一键分享功能】百度一键分享插件
    【WEB2.0】 网页调用QQ聊天(PC+M站)
    python : takes 0 positional arguments but 1 was given
    python : 设计模式之外观模式(Facade Pattern)
    Tosca : 扩展dll动态语言 识别点击下拉, 识别成table
    Tosca : 把 inner text 放到变量里,定义变量,使用变量
    Tosca:键盘输入字符串
    Tosca 给定义变量,取内容放到变量里
    Tosca :配置环境参数
  • 原文地址:https://www.cnblogs.com/y-z-h/p/13933389.html
Copyright © 2011-2022 走看看