首页 > 其他分享 >语法-字符串功能函数

语法-字符串功能函数

时间:2024-06-05 12:31:06浏览次数:9  
标签:函数 cout 语法 str19 字符串 World include size

// 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;
}

标签:函数,cout,语法,str19,字符串,World,include,size
From: https://blog.csdn.net/dh3_zhang/article/details/139440272

相关文章

  • Web前端 函数
    函数函数是一段可以反复调用的代码块函数的声明function命令:function命令声明的代码区块,就是一个函数。function命令后面是函数名,函数名后面是一对圆括号,里面是传入函数的参数。函数体放在大括号里面。functionprint(s){ console.logs(s);}函数名的提升JavaScr......
  • 算法训练营第九天|28. 实现 strStr()459.重复的子字符串 字符串总结 双指针回顾
    28.实现strStr()1.暴力解法:对主串的每一个字符都作为开头,尝试是否匹配字串,时间复杂度O(m*n)2.确保所有的变量在使用前都被明确地初始化了3.kmp算法之后慢慢理解!!!要记得!!!459.重复的子字符串1.暴力解法:列出所有的子字符串,看是否合法(子字符串开头固定),时间复杂度O(n*n)2.用模......
  • printf() 格式字符串的使用方法
    printf()是C语言中一个非常重要的函数,它的核心功能是打印格式化的字符串。而其中的关键则是第一个参数——格式字符串(formatstring)。虽然大多数人都会使用格式字符串,但一些细节可能未必了解。本文将详细说明格式字符串的使用方法。格式字符串(formatstring)格式字符串是......
  • 【sklearn中LinearRegression,logisticregression函数及其参数】
    文章目录前言一、sklearn中的LinearRegression1.引入库2.LinearRegression的主要参数及其解释3.LinearRegression的使用步骤(1)生成模拟数据(2)创建并训练模型(3)预测与评估二、sklearn中的LogisticRegression1.引入库2.LogisticRegression的主要参数及其解释3......
  • 函数图形绘制
    importmatplotlib.pyplotaspltimportnumpyasnpx=np.arange(0,10,0.0001)y1=x**2y2=np.cos(x*2)y3=y1*y2plt.plot(x,y1,linestyle='-.')plt.plot(x,y2,linestyle=':')plt.plot(x,y3,linestyle='--')plt.savef......
  • 分析webpack编译结果, 实现__webpack_require__函数
    分析webpack编译结果,实现__webpack_require__函数本篇文章我们通过手写来分析一下Webpack打包后的代码,并研究一下Webpack是如何将多个模块合并在一起的首先控制台输入npminit-y初始化一个项目,再输入npmiwebpackwebpack-cli-D安装Webpack在src目录想创建入......
  • FreeRTOS基础(十):FreeRTOS任务状态查询API函数介绍
         本篇博客较为基础,介绍时间片调度和常用的任务状态查询API函数接口使用。目录一、时间片调度简介二、任务状态查询API函数介绍2.1常用API函数总览2.2常用API函数介绍2.2.1获取指定任务优先级2.2.2 改变某个任务的任务优先级2.2.3 获取系统中任务的任......
  • java函数笔记
    Statement.executeQuery和Statement.executeUpdate作用:用于执行SQL查询和更新操作。问题:容易导致SQL注入攻击。解决方法:使用PreparedStatement进行参数化查询。//不安全的做法Statementstmt=connection.createStatement();ResultSetrs=stmt.executeQuery("SEL......
  • enumerate()函数的用法与实例
    enumerate()函数是Python中常用的内置函数之一,用于同时遍历集合对象(如列表、元组、字符串等)的索引和元素。用法:enumerate()函数接受一个可迭代对象作为参数,并返回一个生成器对象,每次迭代生成器时,都会返回一个由索引和对应元素值组成的元组。语法:enumerate(iterable,start......
  • js知识点之函数柯里化
    函数柯里化什么是函数柯里化在计算机科学中,柯里化(英语:Currying),又译为卡瑞化或加里化,是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术。这个技术由克里斯托弗·斯特雷奇以逻辑学家哈斯凯尔·加......