本文主要是介绍c++ string使用總結,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主要总结一下字符串常用的一些操作方法
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++ string使用總結的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!