语法-字符串功能函数

// C++标凇库提供了丰富的字符串操作函数,下面介绍一些常用的函数。
// 备注:位置可以看成是字符串的下标,从0开始
// 获取字符串长度
// 使用length或size函数来获取字符串的长度。
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

int main() {
    string str = "Hello, World!";
    size_t length = str.length(); // or str.size()
    cout << "Length of the string: " << length << endl;

    // 拼接字符串
    // 使用+操作符或append函数来拼接字符串。
    string str1 = "Hello";
    string str2 = "World";
    string str3 = str1 + ", " + str2 + "!";
    cout << str3 << endl;

    str1.append(", ").append(str2).append("!");
    cout << str1 << endl;

    // 查找子字符串
    // 使用find函数来查找子字符串的位置。没找到则返回-1
    string str4 = "Hello, World!";
    size_t pos = str4.find("World");
    if (pos != -1) {
        cout << "Found 'World' at position: " << pos << endl;
    } else {
        cout << "'World' not found" << endl;
    }

    // 替换子字符串
    // 使用replace函数来替换子字符串。
    string str5 = "Hello, World!";
    str5.replace(7, 5, "C++"); // 从位置7开始,替换长度为5的子字符串(这5个字符将被删除)
    cout << str5 << endl;// "Hello, C++!"

    // 子字符串提取
    // 使用substr函数提取子字符串。
    string substr = str5.substr(7, 5); // 从位置7开始,提取长度为5的子字符串
    cout << substr << endl;

    // 清空字符串
    // 使用clear函数来清空字符串。
    string str6 = "Hello, World!";
    str6.clear();
    cout << "After clear: " << str6 << endl;

    // 字符串是否为空
    // 使用empty函数检查字符串是否为空。
    string str7 = "";
    if (str7.empty()) {
        cout << "The string is empty" << endl;
    } else {
        cout << "The string is not empty" << endl;
    }

    // 访问字符
    // 使用索引操作符[]或at函数来访问字符串中的字符。
    string str8 = "Hello, World!";
    char ch1 = str8[0]; // 访问第一个字符
    char ch2 = str8.at(1); // 访问第二个字符

    cout << "First character: " << ch1 << endl;
    cout << "Second character: " << ch2 << endl;

    // 插入字符串
    // 使用insert函数在指定位置插入子字符串。
    string str9 = "Hello, World!";
    str9.insert(7, "C++ ");// 在s[7]位置插入字符串
    cout << str9 << endl; // 输出: Hello, C++ World!

    // 删除字符串
    // 使用erase函数删除指定位置的子字符串。
    string str10 = "Hello, C++ World!";
    str10.erase(7, 4); // 从位置7开始删除长度为4的子字符串
    cout << str10 << endl; // 输出: Hello, World!


    // 反转字符串
    // 虽然没有直接的函数,但可以使用标准库算法reverse来反转字符串。
    string str17 = "Hello, World!";
    reverse(str17.begin(), str17.end());
    cout << str17 << endl; // 输出: !dlroW ,olleH

    // 查找字符 - 暂不要求掌握
    // 使用find_first_of和find_last_of函数查找字符串中指定字符的第一个或最后一个位置。
    string str11 = "Hello, World!";
    size_t pos1 = str11.find_first_of('o');
    cout << "First occurrence of 'o': " << pos1 << endl; // 输出: 4

    pos1 = str11.find_last_of('o');
    cout << "Last occurrence of 'o': " << pos1 << endl; // 输出: 8

    // 查找不包含字符 - 暂不要求掌握
    // 使用find_first_not_of和find_last_not_of函数查找字符串中第一个或最后一个不包含指定字符的位置。
    string str12 = "Hello, World!";
    size_t pos2 = str12.find_first_not_of('H');
    cout << "First character not 'H': " << pos2 << endl; // 输出: 1

    pos2 = str12.find_last_not_of('!');
    cout << "Last character not '!': " << pos2 << endl; // 输出: 11

    // 转换大小写 - 暂不要求掌握
    // 没有直接的函数,但可以使用标准库函数来转换字符串中的字符。
    string str13 = "Hello, World!";
    transform(str13.begin(), str13.end(), str13.begin(), ::toupper);
    cout << "Uppercase: " << str13 << endl; // 输出: HELLO, WORLD!

    transform(str13.begin(), str13.end(), str13.begin(), ::tolower);
    cout << "Lowercase: " << str13 << endl; // 输出: hello, world!

    // 比较字符串 - 暂不要求掌握
    // 使用compare函数比较两个字符串。
    string str14 = "Hello";
    string str15 = "World";
    int result = str14.compare(str15);
    if (result < 0) {
        cout << "str14 is less than str15" << endl;
    } else if (result > 0) {
        cout << "str14 is greater than str15" << endl;
    } else {
        cout << "str14 is equal to str15" << endl;
    }

    // 转换为C风格字符串 - 暂不要求掌握
    // 使用c_str函数获取C风格的字符串(即以空字符结尾的字符数组)。
    string str16 = "Hello, World!";
    const char* cstr = str16.c_str();
    cout << cstr << endl; // 输出: Hello, World!

    // 查找子字符串 - 暂不要求掌握
    // 除了find之外,还有rfind函数从右向左查找子字符串。
    string str18 = "Hello, World!";
    size_t pos3 = str18.rfind("World");
    if (pos3 != string::npos) {
        cout << "Found 'World' at position: " << pos3 << endl; // 输出: Found 'World' at position: 7
    } else {
        cout << "'World' not found" << endl;
    }

    // 判断前缀和后缀 - 暂不要求掌握
    // 使用starts_with和ends_with函数判断字符串是否以特定前缀或后缀开头或结尾(C++20)。
    #if __cplusplus >= 202002L // Check if C++20
    string str19 = "Hello, World!";
    bool starts = str19.starts_with("Hello");
    bool ends = str19.ends_with("World!");
    cout << "Starts with 'Hello': " << (starts ? "Yes" : "No") << endl; // 输出: Yes
    cout << "Ends with 'World!': " << (ends ? "Yes" : "No") << endl; // 输出: Yes
    #endif

    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值