文章目录

  • 字符串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;
}

C++常用字符串string方法相关推荐

  1. js常用字符串处理方法

    function getMonth(){var date = new Date();var month=date.getMonth()+1;//当前月份$.ajax({type:"GET&q ...

  2. java字符串string_Java字符串String方法总结

    Java字符串创建与初始化实例.Java字符串String方法总结,包括字符串的大小写替转换,获取字符串长度的方法.截取字符串,去除字符串中的空格.StringBuffer类转换成String类等: ...

  3. python中string什么意思_python字符串(string)方法整理

    C C语言开发 python字符串(string)方法整理 python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitali ...

  4. 如何看待,入门学习Python必看视频?python字符串(string)方法整理

    如何看待,入门学习Python必看视频?哈佛大学教授推荐,python字符串(string)方法整理 哈佛大学推荐,Python基础入门,Python小白书籍,Python学习路线,Python进阶, ...

  5. Java String API 常用的String方法详解

    标题 String类的特性 Java中String类的构造方法 String类的 intern() 注意还跟jdk有关 如何保证变量S指向的是字符串常量池中的数据呢? 关于String中 new St ...

  6. Ruby中有用但不常用的String方法总结

    String中有用但是不常用到的方法: 1).self[substr] 当自身当中包含substr的时候.则生成并返回一致的字符串   irb(main):075:0> substr = &qu ...

  7. Java8几种常用字符串拼接方法总结

    字符串的拼接在Java开发过程中经常被使用,Java中提供了6种常用的字符串拼接方法,本文主要介绍这几种拼接方法的使用 1.使用"+"号 public static void ma ...

  8. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...

    参考链接: python中的字符串string center 作者:骏马金龙 原文地址: https://www.cnblogs.com/f-ck-need-u/p/9127699.html pyth ...

  9. string 方法 java_java中常用的String方法

    1 length()字符串的长度 String a = "Hello Word!"; System.out.println(a.length); 输出的结果是字符串长度10. 2 ...

最新文章

  1. mac本机 Linux服务器anaconda安装
  2. Linux CentOS 7上安装极点五笔
  3. 他是学计算机的这个句子中宾语是动词性的,现代汉语语法部分练习,带答案
  4. 【MM模块】 Cash Discounts 现金折扣
  5. Genymotion如何访问本地服务器?
  6. CSS 中文字体的英文名称对照
  7. Java EE 7中的WebSocket客户端API
  8. springboot springcloud区别_SpringBoot回顾、Spring Cloud初学
  9. vue 横向菜单滚动定位_使用vue组件+iscroll实现一个横向菜单,不能正确滑动
  10. New template: condition
  11. 牛客网——约数的个数
  12. python数据挖掘课程】二十一.朴素贝叶斯分类器详解及中文文本舆情分析
  13. mysql invalid default value_mysql5.x升级到5.7 导入数据出错,提示Invalid default value for...
  14. (四)洞悉linux下的Netfilteramp;iptables:包过滤子系统iptable_filter
  15. 1.PHP7内核剖析 --- PHP 基础架构
  16. 已知X随机变量的情况下,求解Y=G(x)的概率分布函数(只考虑单调的情况)
  17. 医学统计学计算机操作教程第3版附录答案,医学统计学课后习题集答案解析.doc...
  18. ubuntu扩容教程
  19. 怎么从光缆缆标志区别是单模光缆还是多模光缆
  20. 类似支付宝密码框输入

热门文章

  1. Webstorm2019,最新激活码【永久】
  2. Jmeter5.x线程组和Sampler基础组件-第一个测试计划
  3. 程序的编译(详解翻译环境)
  4. 分享一下我的一些学习方法
  5. Java项目:springboot私人牙医管理系统
  6. 大数据专业该学习什么?
  7. 红米联通版刷机包 MIUIV6 扁平化 精简 稳定 流畅 省电 长用版
  8. Spring之ApplicationContext介绍
  9. Aforge做图像处理
  10. sklearn入门——聚类算法KMeans