• string 和 char 数组


    (1)输入输出

    输入运算符“>>”默认会忽略空格,遇到空格就认为输入结束;

    >>  cin string

    <<  cin

    (2) 获取长度

         strlen(str)来获取char数组的实际长度   #include <cstring>

    char str[20]="0123456789";

    int   a = strlen(str);     //a=10;  strlen 计算字符串的长度,以'为字符串结束标记。
    cout << a;

         获取string 实际长度

    s.length()    

    string s = "hello world";

    int len = s.length();

     (3)  访问字符

    String可以像字符串数组一样按照下标来访问其中的每一个字符;

    for (int i = 0; i < s1.length(); i++){
            cout <<s1[i] << endl;

    }

     cout << "迭代方式:" << endl;
        for (string::iterator it = s1.begin(); it != s1.end(); it++){
            cout<<*it<< endl;

    }

    (4)拼接字符串

    +“ 或者”+=“运算符来直接拼接字符串

    string s = s1 + s2;

    s.append(s3);

    (5 ) 相互转换

    使用文件打开函数,必须将string类型的变量转换为字符串数组

     //string -> char*
        string s1 = "far away";
        const char* c = s1.c_str();     // const
        printf("%s ",c);
        //
        string s2 = c;
        //string->char[]
        //从string中赋值字符到char[]
        char arr[50] = {0};
        s1.copy(arr,5,0);
        cout << arr << endl;
     
  • 相关阅读:
    xftp无法用root账号登录问题
    jenkins上gradle打包
    jenkins登录后页面显示为空的问题
    sql 修改oracle数据库中某个字段的部分内容
    redis安装及报错处理
    Centos7 firewall-cmd not found
    ftp connect: No route to host 解决方案
    反向代理负载均衡之Apache
    centos7 openldap双主部署
    apache安装以及报错处理
  • 原文地址:https://www.cnblogs.com/xueshadouhui/p/12938463.html
Copyright © 2020-2023  润新知