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

std::string 常见的操作

在C++中,std::string 是一个非常常用的数据类型,用于处理文本字符串。std::string 提供了丰富的成员函数和操作符,使得字符串处理变得简单而高效。以下是一些常见的 std::string 操作及其示例:

1. 创建字符串

#include <string>
#include <iostream>int main() {std::string s1 = "Hello";       // 直接初始化std::string s2 = s1;            // 复制初始化std::string s3(5, 'a');         // 初始化一个包含5个'a'的字符串std::string s4(s1.begin(), s1.end()); // 从迭代器范围初始化std::cout << s1 << "\n";std::cout << s2 << "\n";std::cout << s3 << "\n";std::cout << s4 << "\n";return 0;
}

2. 获取字符串长度

#include <string>
#include <iostream>int main() {std::string s = "Hello";std::cout << "Length: " << s.length() << "\n";  // 或 s.size()return 0;
}

3. 访问字符串中的字符

#include <string>
#include <iostream>int main() {std::string s = "Hello";std::cout << "First character: " << s[0] << "\n";std::cout << "Last character: " << s[s.length() - 1] << "\n";return 0;
}

4. 连接字符串

#include <string>
#include <iostream>int main() {std::string s1 = "Hello";std::string s2 = "World";std::string s3 = s1 + " " + s2;std::cout << s3 << "\n";return 0;
}

5. 比较字符串

#include <string>
#include <iostream>int main() {std::string s1 = "Hello";std::string s2 = "World";std::string s3 = "Hello";if (s1 == s2) {std::cout << "s1 and s2 are equal\n";} else {std::cout << "s1 and s2 are not equal\n";}if (s1 == s3) {std::cout << "s1 and s3 are equal\n";}if (s1 < s2) {std::cout << "s1 is less than s2\n";}return 0;
}

6. 查找子字符串

#include <string>
#include <iostream>int main() {std::string s = "Hello World";size_t pos = s.find("World");if (pos != std::string::npos) {std::cout << "Found 'World' at position: " << pos << "\n";} else {std::cout << "'World' not found\n";}return 0;
}

7. 替换子字符串

#include <string>
#include <iostream>int main() {std::string s = "Hello World";s.replace(s.find("World"), 5, "C++");std::cout << s << "\n";return 0;
}

8. 插入和删除字符

#include <string>
#include <iostream>int main() {std::string s = "Hello";s.insert(5, " World");  // 在第5个位置插入" World"s.erase(5, 6);          // 从第5个位置开始删除6个字符std::cout << s << "\n";return 0;
}

9. 子字符串

#include <string>
#include <iostream>int main() {std::string s = "Hello World";std::string sub = s.substr(6, 5);  // 从第6个位置开始提取5个字符std::cout << sub << "\n";return 0;
}

10. 字符串流

#include <string>
#include <sstream>
#include <iostream>int main() {std::string s = "123";int num;std::stringstream ss(s);ss >> num;std::cout << "Number: " << num << "\n";return 0;
}

这些是 std::string 中一些最常见的操作。通过这些操作,你可以轻松地进行字符串的创建、访问、连接、比较、查找、替换、插入、删除和提取子字符串等操作。


http://www.mrgr.cn/news/30267.html

相关文章:

  • 有毒有害气体检测仪的应用和性能_鼎跃安全
  • 百度营销转化追踪(网页JS布码)
  • gazebo 仿真阶段性问题汇总二
  • C# 携手 7-Zip 命令行:大文件压缩的终极武器?
  • 电脑装系统装错了盘怎么恢复文件?全方位指南
  • 排序题目:三次操作后最大值与最小值的最小差
  • 智能车镜头组入门(四)元素识别
  • 图片翻译器,分享四款直接翻译图片的软件!
  • SourceTree保姆级教程3:(分支创建 及 合并)
  • 深入探讨 JVM 内存泄漏与溢出:定位与解决方案
  • VirtualBox7.1.0 安装 Ubuntu22.04.5 虚拟机
  • 【机器学习】OpenCV高级图像处理
  • 睢宁自闭症寄宿学校:培养特殊孩子的无限潜能
  • 【科技论文写作与发表】论文分类
  • springboot通过tomcat部署项目(包含jar、war两种方式,迄今为止全网最详细!2024更新..建议收藏,教学!万字长文!)
  • 当 PC 端和移动端共用一个域名时,避免 CDN 缓存页面混乱(nginx)
  • C++:类和对象全解
  • 海康威视摄像机和录像机的监控与回放
  • 【安当产品应用案例100集】017-助力软件服务商高效集成多因素认证
  • MySql调优(三)Query SQL优化(2)explain优化