c++字符串操作入门
2018-01-07 17:43
281 查看
主要总结一下字符串常用的一些操作方法
1、这里以举个栗子,问题描述如下:如何将字符串:“one,two,three”,中的所有逗号替换成“、”?
代码如下:
#include <iostream> #include <string> int main() { std::string sStr = "one,two,three"; const std::string sStop = ","; //size_type为位置大小的类型,find用来查找某个字串在字符串中的位置 //除了find常用外,find_first_of(substr)用来查找某个字串的第一次出现的位置 std::string::size_type pos = sStr.find(sStop); //npos为string类的成员变量,表示字符串结尾位置 while (pos != std::string::npos) { sStr.replace(pos, sStop.length(), "、"); pos = sStr.find(sStop, pos + 1); } std::cout << sStr <<std::endl; }
ps:
想了解更多string类的成员函数(C++),参见:
http://www.cplusplus.com/reference/string/string/
想了解更多的字符串处理函数(C),参见: http://www.cplusplus.com/reference/cstring/ or
http://www.runoob.com/cplusplus/cpp-strings.html(C)(菜鸟教程)
-未完待续-
相关文章推荐
- C++里面混乱的字符串操作
- C++实验6——字符串操作
- C++ 字符串操作经验集
- C++字符串string类常用操作详解(一)【初始化、遍历、连接】
- Lua教程(五):C/C++操作Lua数组和字符串示例
- C++_String_类字符串操作
- Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】[原创]_python_脚本之家
- C++字符串find操作
- C++字符串操作详解
- C++入门⑥:字符串
- C++ 字符串操作经验集
- [C++ 学习] C++ Primer 第4版 习题 4.30 字符串操作 & 我对堆内存 和 常量区内存的一些理解
- javascript入门经典---笔记7 字符串操作(1)
- C++ ## 宏定义中字符串连接操作
- C++字符串几个操作
- 双向循环链表实现字符串或int类型数组的左移或右移操作C++
- c++入门:输入一个字符串,将其分段输出,分段符为除字母和数字之外的符号。
- c++概念字符串操作 (转)
- C++第六次作业-数组操作、字符串操作
- 给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.4 -- 4.5