C++中string的常用函数

常用的 std::string 成员函数

1. empty()

检查字符串是否为空。

#include <iostream>
#include <string>

int main() {
    std::string str = "";
    if (str.empty()) {
        std::cout << "字符串为空。" << std::endl;
    } else {
        std::cout << "字符串不为空。" << std::endl;
    }
    return 0;
}

2. clear()

清空字符串内容。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    str.clear();
    std::cout << "清空后的字符串长度: " << str.size() << std::endl;
    return 0;
}

3. append() 和 operator+=()

向字符串末尾添加内容。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    str.append(", World!"); // 使用 append()
    // 或者使用 += 操作符
    str += " How are you?";
    std::cout << str << std::endl;
    return 0;
}

4. substr()

获取字符串的子串。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string sub = str.substr(7, 5); // 从索引7开始,长度为5
    std::cout << "子串: " << sub << std::endl; // 输出 "World"
    return 0;
}

5. find() 和 rfind()

查找子字符串的位置。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    size_t pos = str.find("World");
    if (pos != std::string::npos) {
        std::cout << "\"World\" 在位置: " << pos << std::endl;
    }
    pos = str.rfind("o"); // 从后往前查找 'o'
    if (pos != std::string::npos) {
        std::cout << "最后一个 'o' 在位置: " << pos << std::endl;
    }
    return 0;
}

6. replace()

替换字符串中的部分内容。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    str.replace(7, 5, "C++"); // 从索引7开始,长度为5,替换为 "C++"
    std::cout << str << std::endl; // 输出 "Hello, C++!"
    return 0;
}

7. insert()

在指定位置插入内容。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    str.insert(7, "beautiful "); // 在索引7处插入 "beautiful "
    std::cout << str << std::endl; // 输出 "Hello, beautiful World!"
    return 0;
}

8. erase()

删除字符串中的部分内容。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    str.erase(5, 7); // 从索引5开始,删除7个字符
    std::cout << str << std::endl; // 输出 "Hello!"
    return 0;
}

9. c_str() 和 data()

获取 C 风格的字符串指针。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    const char* cstr = str.c_str(); // 获取 C 风格的字符串
    std::cout << cstr << std::endl;
    return 0;
}

10. compare()

比较两个字符串。

#include <iostream>
#include <string>

int main() {
    std::string str1 = "apple";
    std::string str2 = "banana";

    if (str1.compare(str2) < 0) {
        std::cout << "apple 在 banana 之前。" << std::endl;
    } else if (str1.compare(str2) > 0) {
        std::cout << "apple 在 banana 之后。" << std::endl;
    } else {
        std::cout << "两个字符串相等。" << std::endl;
    }

    return 0;
}

11. at() 和 operator[]

访问或修改指定位置的字符。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    char ch = str.at(1); // 使用 at() 访问
    str[1] = 'a'; // 使用 operator[] 修改
    std::cout << str << " " << ch << std::endl; // 输出 "Hallo e"
    return 0;
}

12. resize()

调整字符串的大小。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    str.resize(10, '!'); // 扩展到长度10,用 '!' 填充
    std::cout << str << std::endl; // 输出 "Hello!!!!!"
    return 0;
}

13. copy()

将字符串的一部分复制到一个字符数组中。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    char buffer[6];
    str.copy(buffer, 5, 7); // 从索引7开始复制5个字符
    buffer[5] = '\0'; // 添加 null 终止符
    std::cout << buffer << std::endl; // 输出 "World"
    return 0;
}

14. begin(), end(), rbegin(), rend()

获取字符串的迭代器。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";

    // 正向迭代
    for(auto it = str.begin(); it != str.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 反向迭代
    for(auto it = str.rbegin(); it != str.rend(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

15. pop_back()

删除字符串的最后一个字符。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello!";
    str.pop_back(); // 删除最后一个字符
    std::cout << str << std::endl; // 输出 "Hello"
    return 0;
}

16. swap()

交换两个字符串的内容。

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";
    str1.swap(str2);
    std::cout << "str1: " << str1 << ", str2: " << str2 << std::endl; // 输出 "str1: World, str2: Hello"
    return 0;
}

17. find_first_of() 和 find_last_of()

查找字符串中任意一个字符的位置。

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string chars = "aeiou";
    size_t pos = str.find_first_of(chars); // 查找第一个元音字母的位置
    if (pos != std::string::npos) {
        std::cout << "第一个元音字母在位置: " << pos << std::endl;
    }
    pos = str.find_last_of(chars); // 查找最后一个元音字母的位置
    if (pos != std::string::npos) {
        std::cout << "最后一个元音字母在位置: " << pos << std::endl;
    }
    return 0;
}

18. find_first_not_of() 和 find_last_not_of()

查找字符串中第一个不在指定字符集中的字符的位置。

#include <iostream>
#include <string>

int main() {
    std::string str = "!!!Hello!!!";
    size_t pos = str.find_first_not_of('!'); // 查找第一个不是 '!' 的字符
    if (pos != std::string::npos) {
        std::cout << "第一个非 '!' 字符在位置: " << pos << std::endl;
    }

    pos = str.find_last_not_of('!'); // 查找最后一个不是 '!' 的字符
    if (pos != std::string::npos) {
        std::cout << "最后一个非 '!' 字符在位置: " << pos << std::endl;
    }

    return 0;
}

19. 转换大小写

使用 std::transform 和 std::toupper 或 std::tolower 来转换字符串的大小写。

#include <iostream>
#include <string>
#include <algorithm> // For std::transform
#include <cctype>    // For std::toupper

int main() {
    std::string str = "Hello, World!";
    
    // 转换为大写
    std::transform(str.begin(), str.end(), str.begin(), ::toupper);
    std::cout << "大写: " << str << std::endl; // 输出 "HELLO, WORLD!"

    // 转换为小写
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    std::cout << "小写: " << str << std::endl; // 输出 "hello, world!"

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Every DAV inci

小辣鸡一枚,不求打赏啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值