美文网首页
c++ string使用

c++ string使用

作者: 简书网abc | 来源:发表于2024-01-09 23:07 被阅读0次
// #include "lib/sundry.hpp"
#include <future>
#include <iostream>

using namespace std;

void test01() {
    string str1("hello world");
    cout << str1 << endl;

    string str2(5, 'a');
    cout << str2 << endl;

    string str4;
    str4 = "hello world!";
    cout << str4 << endl;
    str4 = 'w';
    cout << str4 << endl;

    string str5 = "hello world";
    str4.assign(str5, 2, 3);    // string成员函数
    cout << str4 << endl;
    cout << "test01 end ... " << endl;
}

void test02() {
    string str1 = "hello world";
    cout << str1[1] << " -- " << str1.at(6) << endl;
    str1[1] = 'E';              // []越界不会抛出异常.
    str1.at(6) = 'H';           // at方法,越界会抛出异常.
    cout << str1 << endl;
    
    try{
//        str1[100] = 'a';
        str1.at(100) = 'a';         // 字符串异常捕获.
    } catch (exception &e) {
        cout << "捕获到异常: " << e.what() << endl;
    }
    
    cout << "test02 end ... " << endl;
}

void test03() {
    string str1 = "hello";
    str1 += " world!";
    cout << str1 << endl;

    string str2 = "hello-";
    string str3 = "world";
    str2.append(str3, 1, 2);    // 字符串追加
    cout << str2 << endl;

    cout << "test03 end ..." << endl;
}

void test04() {
    string str1 = "http://www.word.888.word.com.word";
    cout << str1 << endl;
    while (1) {
        int ret = str1.find("word"); // 字符串查找, 返回查找字符串的位置
        cout << ret << endl;
        if ( ret < 0) {
            break;
        }
        str1.replace(ret, 4, "***");    // 字符串替换.
    }
    cout << str1 << endl;
    cout << "test04() end ..." << endl;
}

void test05() {
    string s = "This Is An Example";
    cout << "1) " << s << '\n';

    s.erase(7, 3); // 使用重载 (1) 擦除 " An"
    cout << "2) " << s << '\n';

    s.erase(s.find(' ')); // 使用重载 (1) 截掉从 ' ' 到字符串结尾的部分
    cout << "4) " << s << '\n';
    cout << "test05() end ..." << endl;
}

void test06() {
    // char *转为 string (默认支持)
    string str1;
    str1 = (string)"hello";
    cout << str1 <<endl;

    // string 不能直接转为char *, 需要使用成员函数 c_str()
    string str2 = "hello";
    char *p = (char *)str2.c_str();
    cout << p <<endl;
    cout << "test06() end ..." << endl;
}


int main() {
    test01();
    test02();
    test03();
    test04();
    test05();
    test06();
    return 0;
}




相关文章

  • c++中的字符串string和C语言中的字符char

    c++中的字符串string 在c++中使用string类,必须在程序中包含头文件string #include ...

  • [C++之旅] 8 string类型的使用

    [C++之旅] 8 string类型的使用 使用string需包含#include 头文件 初始化string对...

  • string的用法

    1、使用string头文件 要想使用标准C++中string类,必须要包含: 或者 string和wstring的...

  • string对象

    string类型是c++标准模板库设计的数据类型,专门用于字符串处理.在使用c++编程时,优先考虑使用string...

  • 触宝内推-substring-c++

    push_back使用方法C++中string.find()函数与string::npos

  • 2018-05-30 C++& lasagne

    C++ 1.C++中string erase函数的使用erase函数的原型如下: string & erase(s...

  • c/c++中把数字变为字符串

    int 转化为 string 使用sprintf().[在C和C++中均可用] 使用stringstream C+...

  • leetcode6 关于C++ string

    C++中string的输入 使用scanf输入(未自己验证) 首先声明string; 分配空间; 输入首地址; 使...

  • C++--string

    要想使用标准C++中string类,必须要包含 或 下面你就可以使用string/wstring了,它们两分别对应...

  • C++ string的使用

    为了理解C++ string类的必要性,需要考虑C风格字符串的优势和劣势。优势: 很简单,底层使用了基本的字符类型...

网友评论

      本文标题:c++ string使用

      本文链接:https://www.haomeiwen.com/subject/grwandtx.html