C++\String常用操作

#include <iostream>
#include <string>
using namespace std;
int main()
{
    // string类的字符串链接以及初始化赋值
    string a="a";
    string b="b";
    string c=a+b;            // string类的字符串链接以及初始化赋值

    //string的输出
    cout<<"String输出方法一:"<<c<<endl;
    cout<<"String输出方法二:";
    for(int i=0;i<c.length();i++)
        cout<<c[i];
    cout<<endl;

    //另外一种初始化方法
    c.assign("abcd", 3);
    cout<<c<<endl;

    //两个字符交换,swap
    a.swap(b);
    cout<<"字符串a为:"<<a<<endl;
    cout<<"字符串b为:"<<b<<endl;

    //添加字符串的方法
    c.append("d");
    cout<<"c为:"<<c<<endl;
    c+="e";
    cout<<"c为:"<<c<<endl;

    //字符串的插入
    c.insert(0,"h");
    c.insert(c.length(),"t");
    cout<<c<<endl;

    //字符串的替换replace和清除erase
    c.replace(1,2,"AB");                  //第一个参数为从哪一个开始 第二个参数为替换几个,第三个为替换内容
    cout<<"替换后的c为:"<<c<<endl;
    c.erase(1,3);                         //erase(i,j);清除i到j的字符 下标从0开始
    cout<<"清除后的c为:"<<c<<endl;      

    //字符串的比较
    string s1="123";
    string s2="123";
    string s3="1234";
    string s4="12";
    if(s1==s2)
        cout<<"字符串s1和字符串s2相同!"<<endl;
    if(s3>s2)
        cout<<"s3>s2!"<<endl;
    if(s4<s2)
        cout<<"s4<s2!"<<endl;

    //string和int的相互转换!!!
    string S="852";
    char tm[12];
    int td=atoi(S.c_str());                            //string->int
    cout<<"S转换为int后为:"<<td<<endl;
    itoa(td,tm,10);                                    //int->string,(要转换的int,目标字符串数组,进制)
    string S1=tm;
    cout<<"td转换为string后为:"<<S1<<endl;
    system("pause");
}
### C++ 中 `string` 的常用操作方法 #### 1. 基本声明与初始化 在 C++ 中,可以使用多种方式来定义和初始化字符串对象。以下是常见的几种形式[^2]: - 使用默认构造函数创建空字符串: ```cpp std::string str; ``` - 使用常量字符数组初始化字符串: ```cpp std::string str1 = "hello"; ``` - 动态分配并初始化字符串: ```cpp std::string* str2 = new std::string("hello"); delete str2; // 记得释放动态内存 ``` - 自定义重复字符的数量初始化字符串: ```cpp std::string str3(5, 'a'); // 创建包含 5 个 'a' 字符的字符串 ``` #### 2. 获取字符串长度 可以通过成员函数 `.length()` 或者 `.size()` 来获取字符串中的字符数量。 ```cpp std::string str = "example"; int length = str.length(); // 结果为7 ``` #### 3. 子串提取 通过调用 `.substr(pos, len)` 函数可以从指定位置截取子串。 ```cpp std::string str = "abcdefghi"; std::string subStr = str.substr(3, 4); // 截取从索引3开始的4个字符:"defg" ``` #### 4. 查找子串或字符 `.find(sub_str)` 和 `.rfind(sub_str)` 是用于查找子串或者单个字符的方法。如果找到返回其起始位置;如果没有找到,则返回特殊值 `std::string::npos`。 ```cpp std::string str = "hello world!"; size_t pos = str.find("world"); // 找到 "world" 开头的位置:6 if (pos != std::string::npos){ std::cout << "Found at position: " << pos << '\n'; } ``` #### 5. 替换部分字符串 利用 `.replace(start_pos, num_chars_to_replace, replacement_string)` 实现替换功能。 ```cpp std::string str = "abcdef"; str.replace(1, 3, "XYZ"); // 将索引1处开始的三个字符替换成 "XYZ" // 修改后的字符串变为 aXYZf ``` #### 6. 插入删除字符/子串 支持向现有字符串中插入新内容以及移除部分内容的操作。 - **插入**: ```cpp std::string str = "abcd"; str.insert(2, "XY"); // 在索引2之前插入 "XY", 得到 abXYcd ``` - **删除**: ```cpp std::string str = "abcdef"; str.erase(1, 3); // 删除从索引1开始的3个字符,得到 adf ``` #### 7. 翻转字符串 虽然标准库未提供直接针对 `std::string` 的翻转方法,但可借助 `<algorithm>` 头文件里的通用算法完成此任务[^3]。 ```cpp #include <iostream> #include <algorithm> std::string s2 = "12345"; std::reverse(s2.begin(), s2.end()); std::cout << s2 << "\n"; // 输出结果为 54321 ``` #### 8. 比较两个字符串是否相等 除了常规比较运算符外,还可以采用 `.compare(anotherString)` 进行更灵活的对比。 ```cpp std::string str1 = "test"; std::string str2 = "Test"; if(str1.compare(str2) == 0){ std::cout << "Strings are equal.\n"; } else { std::cout << "Strings differ.\n"; } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值