12.C++ string 操作

简介:
字符串初始化
void main() {
    string s1 = "111111";
    string s2("22222");
    string s3 = s2;
    cout << s1<< endl;
    cout << s2 << endl;
    cout << s3 << endl;
}
字符串遍历

使用[]和使用at()遍历的差别在于,at()发生数组越界会抛出异常,可以捕获,但是[]方式发生异常无法进行捕获

//通过角标取值
void main() {
    string  s1 = "abcdefg";
    for (int i = 0; i < s1.length(); i++){
        cout << s1[i];
    }
}

//通过at()取值
void main() {
    string  s1 = "abcdefg";
    for (int i = 0; i < s1.length(); i++){
        cout << s1.at(i);
    }
}

//使用迭代器遍历
void main() {
    string  s1 = "abcdefg";
    for (string::iterator i = s1.begin(); i != s1.end(); i++) {
        //迭代器可以看作指针
        cout << *i<< endl;
    }
}
string char的转换
void main(){
    string s1 = "aaabbbccc";
    // to char
    cout << s1.c_str()<<endl;
    char buf[128] = {0};
    s1.copy(buf, 9, 0);
    cout <<"buf="<< buf<< endl;
}
string链接
void main() {
    string s2 = "aaa";
    string s3 = "bbb";
    s2 = s2 + s3;
    cout << "s2 = "<<s2<< endl;

    string s4 = "222";
    string s5 = "444";
    s4.append(s5);
    cout << "s4="<<s4<< endl;
}
查找和替换
void main() {
    string s = "who are you and who is she and who am i";
        //从0开始查找
    int index = s.find("who", 0);
    while (index != string::npos) {
        cout << "index="<<index << endl;
        //替换,从0开始,替换三个字符
        s.replace(index, 3, "WHOSE");
        index = index + 1;
        index = s.find("who", index);
    }
    cout << "替换后:"<<s << endl;
}
删除字符
//找到指定字符,然后删除
void main() {
    string s1 = "hello1 hello2 hello3";
    string ::iterator it = find(s1.begin(), s1.end(), '1');
    if (it != s1.end()) {
        s1.erase(it);
    }
    cout << "删除之后:"<<s1<< endl;
    system("pause");
}

//删除指定范围的字符
void main() {
    string s1 = "hello1hello2hello3";
    s1.erase(1, 5);
    cout << "删除1到5位置的字符后:" << s1 << endl;
    system("pause");
}
插入字符
void main() {
    string s1 = "how are you today";
    s1.insert(0, "hello ");
    cout << "插入后:" << s1 << endl;
    system("pause");
}
大小写转换
void main() {
    string s1 = "how are you today";
    transform(s1.begin(), s1.end(), s1.begin(), toupper);
    cout << "转换后:" << s1 << endl;
    string s2 = "I AM GOOD";
    transform(s2.begin(), s2.end(), s2.begin(), tolower);
    cout << "转换后:" << s2 << endl;
    system("pause");
}
相关文章
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
38 0
|
18天前
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
1月前
|
存储 JSON 安全
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
66 0
|
28天前
|
存储 编译器 C语言
C++_String增删查改模拟实现
C++_String增删查改模拟实现
47 0
|
6天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
19天前
|
存储 算法 C语言
【C++初阶】8. STL初阶 + String类
【C++初阶】8. STL初阶 + String类
48 1
|
19天前
|
C语言 C++
【C++初阶】9. string类的模拟实现
【C++初阶】9. string类的模拟实现
38 1
|
24天前
|
NoSQL C++
c++中包含string成员的结构体拷贝导致的double free问题
c++中包含string成员的结构体拷贝导致的double free问题
8 0