std::string 常见的操作

在C++中,std::string 是一个非常常用的数据类型,用于处理文本字符串。std::string 提供了丰富的成员函数和操作符,使得字符串处理变得简单而高效。以下是一些常见的 std::string 操作及其示例:

1. 创建字符串

#include <string>
#include <iostream>

int main() {
    std::string s1 = "Hello";       // 直接初始化
    std::string s2 = s1;            // 复制初始化
    std::string s3(5, 'a');         // 初始化一个包含5个'a'的字符串
    std::string s4(s1.begin(), s1.end()); // 从迭代器范围初始化

    std::cout << s1 << "\n";
    std::cout << s2 << "\n";
    std::cout << s3 << "\n";
    std::cout << s4 << "\n";

    return 0;
}

2. 获取字符串长度

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello";
    std::cout << "Length: " << s.length() << "\n";  // 或 s.size()
    return 0;
}

3. 访问字符串中的字符

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello";
    std::cout << "First character: " << s[0] << "\n";
    std::cout << "Last character: " << s[s.length() - 1] << "\n";
    return 0;
}

4. 连接字符串

#include <string>
#include <iostream>

int main() {
    std::string s1 = "Hello";
    std::string s2 = "World";
    std::string s3 = s1 + " " + s2;
    std::cout << s3 << "\n";
    return 0;
}

5. 比较字符串

#include <string>
#include <iostream>

int main() {
    std::string s1 = "Hello";
    std::string s2 = "World";
    std::string s3 = "Hello";

    if (s1 == s2) {
        std::cout << "s1 and s2 are equal\n";
    } else {
        std::cout << "s1 and s2 are not equal\n";
    }

    if (s1 == s3) {
        std::cout << "s1 and s3 are equal\n";
    }

    if (s1 < s2) {
        std::cout << "s1 is less than s2\n";
    }

    return 0;
}

6. 查找子字符串

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello World";
    size_t pos = s.find("World");
    if (pos != std::string::npos) {
        std::cout << "Found 'World' at position: " << pos << "\n";
    } else {
        std::cout << "'World' not found\n";
    }
    return 0;
}

7. 替换子字符串

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello World";
    s.replace(s.find("World"), 5, "C++");
    std::cout << s << "\n";
    return 0;
}

8. 插入和删除字符

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello";
    s.insert(5, " World");  // 在第5个位置插入" World"
    s.erase(5, 6);          // 从第5个位置开始删除6个字符
    std::cout << s << "\n";
    return 0;
}

9. 子字符串

#include <string>
#include <iostream>

int main() {
    std::string s = "Hello World";
    std::string sub = s.substr(6, 5);  // 从第6个位置开始提取5个字符
    std::cout << sub << "\n";
    return 0;
}

10. 字符串流

#include <string>
#include <sstream>
#include <iostream>

int main() {
    std::string s = "123";
    int num;

    std::stringstream ss(s);
    ss >> num;

    std::cout << "Number: " << num << "\n";

    return 0;
}

这些是 std::string 中一些最常见的操作。通过这些操作,你可以轻松地进行字符串的创建、访问、连接、比较、查找、替换、插入、删除和提取子字符串等操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值