当前位置: 首页 > news >正文

C++常用字符串string方法

文章目录

  • 字符串string操作方法
    • 1. 类方法
      • 使用示例
    • 2. 头文件cstring方法
      • 使用示例

字符串string操作方法

1. 类方法

在C++中,引入string.h头文件可以使用C语言中的字符串操作函数。然而,C++提供了一个更加方便的字符串类string,不需要引入string.h头文件,可以直接使用其提供的方法。

后续第二部分会引入<cstring>的方法。<string.h>和<cstring>这两个头文件唯一的区别是,cstring是C++标准库中的头文件,而string.h是C标准库中的头文件。

以下是string类中常用的方法:

  1. length():返回字符串的长度。

  2. size():返回字符串中字符的个数。

  3. at(index):返回字符串中指定位置的字符。

  4. substr(start, length):返回字符串中从指定位置开始的指定长度的子字符串。

  5. append(str):将指定的字符串追加到当前字符串的末尾。

  6. insert(pos, str):在指定位置插入指定的字符串。

  7. erase(pos, length):从指定位置开始删除指定长度的字符。

  8. replace(pos, length, str):用指定的字符串替换从指定位置开始的指定长度的字符。

  9. find(str):在字符串中查找指定的子字符串。find()方法返回的是子字符串在字符串中的位置,如果找不到则返回string::npos

  10. compare(str):比较两个字符串是否相等。

使用示例

#include <iostream>
#include <string>using namespace std;int main() {string str = "Hello World!";// 使用length()方法获取字符串的长度int len = str.length();cout << "Length of str: " << len << endl; // 输出:Length of str: 12// 使用size()方法获取字符串中字符的个数int size = str.size();cout << "Size of str: " << size << endl; // 输出:Size of str: 12// 使用at()方法获取指定位置的字符char ch = str.at(6);cout << "Character at position 6: " << ch << endl; // 输出:Character at position 6: W// 使用substr()方法获取子字符串string subStr = str.substr(6, 5);cout << "Sub-string: " << subStr << endl; // 输出:Sub-string: World// 使用append()方法将字符串追加到末尾str.append(" Goodbye!");cout << str << endl; // 输出:Hello World! Goodbye!// 使用insert()方法在指定位置插入字符串(之前)str.insert(6, "there ");cout << str << endl; // 输出:Hello there World! Goodbye!// 使用erase()方法删除指定位置的字符str.erase(5, 6);cout << str << endl; // 输出:New string: Hello World! Goodbye!// 使用replace()方法替换指定位置的字符str.replace(6, 5, "there");cout << str << endl; // 输出:New string: Hello there! Goodbye!// 使用find()方法查找子字符串size_t pos = str.find("World");if (pos != string::npos) {cout << "Found 'World' at position " << pos << endl; } else {cout << "Unable to find 'World'" << endl;// 输出:Unable to find 'World' }// 使用find()方法查找子字符串pos = str.find("bye");if (pos != string::npos) {cout << "Found 'bye' at position " << pos << endl; // 输出:Found 'bye' at position 17} else {cout << "Unable to find 'bye'" << endl;}// 使用compare()方法比较两个字符串是否相等string str1 = "Hello";string str2 = "hello";int result = str1.compare(str2);if (result == 0) {cout << "Strings are equal" << endl;} else if (result < 0) {cout << "str1 is less than str2" << endl; // √ } else {cout << "str1 is greater than str2" << endl;}return 0;
}

2. 头文件cstring方法

C++中可以使用以下函数来操作字符串:

  1. strlen():返回一个字符串的长度。

  2. strcpy():将一个字符串复制到另一个字符串中。

  3. strcat():将一个字符串追加到另一个字符串的末尾。

  4. strcmp():用于比较两个字符串是否相等。

  5. strstr():在一个字符串中查找另一个字符串。

  6. strtok():用于将一个字符串分割成多个子字符串。

  7. tolower():将字符串中的所有字符转换为小写字母。

  8. toupper():将字符串中的所有字符转换为大写字母。

  9. isalpha():用于判断一个字符是否为字母。

  10. isdigit():用于判断一个字符是否为数字。

这些函数都是C++标准库中提供的字符串操作函数,非常常用。

使用示例

#include <iostream>
#include <cstring>using namespace std;int main()
{char str1[20] = "Hello";char str2[20] = "World";char str3[20];// 使用strcpy将str1复制到str3中strcpy(str3, str1);cout << "str3: " << str3 << endl; // 输出:str3: Hello// 使用strcat将str2追加到str3的末尾strcat(str3, str2);cout << "str3: " << str3 << endl; // 输出:str3: HelloWorld// 使用strcmp比较str1和str2int result = strcmp(str1, str2);if (result < 0){cout << "str1 is less than str2" << endl; // 输出:str1 is less than str2}else if (result > 0){cout << "str1 is greater than str2" << endl;}else{cout << "str1 is equal to str2" << endl;}// 使用strstr在str3中查找"World"char* ptr = strstr(str3, "World");cout << "ptr: " << ptr << endl; // 输出:ptr: World// 使用strtok将str3分割成多个子字符串char* token = strtok(str3, " ");while (token != NULL){cout << token << endl;token = strtok(NULL, " ");}// 输出:// HelloWorldreturn 0;
}
http://www.lryc.cn/news/45222.html

相关文章:

  • XML树结构和语法
  • 【Qt】Qt单元测试详解(四):Google Test 断言
  • 句柄和指针的区别
  • Linux 网络编程学习笔记——十四、多线程编程
  • JS 获取时区
  • 【0183】PG内核客户端认证之将读取的token创建HbaToken(3 - 1)
  • 别把 OpenAI 太当回事,它远未达到替换前端的地步
  • 前端基础HTML、CSS--8(CSS-5)
  • 基于ASP网络办公OA系统的设计与实现
  • C语言计算机二级/C语言期末考试 刷题(五)
  • 2023-04-03 grafana-源码编译启动及添加prometheus数据源
  • 微软New Bing(GPT-4)写的Delphi诗歌
  • 【进程地址空间】
  • 递归dfs入门
  • 华为OD机试用java实现 -【吃火锅】
  • AI创作优美文章的秘密大揭秘!
  • SpringMVC的拦截器
  • dolphinscheduler-3.1.4
  • 大前端05-用vue轻量级第三方组件库快速创建个画板,可以支持画板、直线、圆形等输入,可以撤回,改变颜色
  • ChatGPT使用案例之生成PPT
  • ChatGPT基础知识系列之模型介绍
  • ChatGPT助力软件开发
  • 这些关于高压放大器的常识,你知道多少?(二)
  • 使用神经网络中的卷积核生成语谱图
  • 文章五:Python 网络爬虫实战:使用 Beautiful Soup 和 Requests 抓取网页数据
  • 【大数据之Hadoop】八、MapReduce之序列化
  • Python网络爬虫之Selenium详解
  • 中睿天下受邀出席电促会第五次会员代表大会
  • Chat GPT:软件测试人员的危机?
  • 【Redis】高可用:Redis的主从复制是怎么实现的?