您的位置:首页 > 编程语言 > C语言/C++

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)(菜鸟教程)

-未完待续-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: