字符串string(分割与融合)
来源:互联网 发布:北京外国语大学知乎 编辑:程序博客网 时间:2024/05/23 14:08
<span style="font-size:18px;">#include <iostream>#include <string>#include <vector>using namespace std;vector<string> split(string str,string pattern) { string::size_type pos; vector<string> result; str+=pattern;//扩展字符串以方便操作 string::size_type size=str.size(); for(string::size_type i=0; i<size; i++) { pos=str.find(pattern,i); //find的用法,返回一个size_type,及pattern的位置 if(pos<size) { string s=str.substr(i,pos-i); result.push_back(s); i=pos+pattern.size()-1; //pattern.size()这个是为了防止多个出现的情况 } } return result; }int main(){string a = "abcde fgeh ijkl";vector<string> vec;vec = split(a, "ge");vector<string>::iterator iter = vec.begin();for( ; iter!=vec.end(); iter++)cout<<*iter<<endl;return 0;}</span></span>
string类的查找函数: int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置//查找成功时返回所在位置,失败返回string::npos的值 int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置int find_first_of(const char *s, int pos = 0) const;int find_first_of(const char *s, int pos, int n) const;int find_first_of(const string &s,int pos = 0) const;//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos int find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_first_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const;//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos int find_last_of(char c, int pos = npos) const;int find_last_of(const char *s, int pos = npos) const;int find_last_of(const char *s, int pos, int n = npos) const;int find_last_of(const string &s,int pos = npos) const; int find_last_not_of(char c, int pos = npos) const;int find_last_not_of(const char *s, int pos = npos) const;int find_last_not_of(const char *s, int pos, int n) const;int find_last_not_of(const string &s,int pos = npos) const;//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
basic_string substr(size_type _Off = 0,size_type _Count = npos) const;
功能:从一个字符串复制一个从指定位置开始,并具有指定长度的子字符串。
0 0
- 字符串string(分割与融合)
- Java字符串(String)分割方法
- Java字符串(String)分割方法
- 分割字符串 String类spilt()函数
- string 字符串分割
- String.split() 字符串分割
- [C++]string分割字符串
- java ,String分割字符串
- std::string字符串分割
- lua string 分割字符串
- string字符串分割
- string 分割字符串
- C++ string 分割字符串
- 字符串的分割String
- C++ 字符串融合 和 string 与 int 之间最简单的转换方法
- String split分割字符串问题
- string 分割字符串 2011.07.13
- Lua string split分割字符串
- 题目1080:进制转换(2008年清华大学计算机研究生机试真题)ps:用java太简单了
- 使用 Python SimpleHTTPServer 快速共享文件
- 一篇糟糕的实验报告
- 事件过滤器
- SQL Server 和Oracle 数据类型对应
- 字符串string(分割与融合)
- printf()用%f输出double型,scanf用%lf
- 题目1012:畅通工程(2005年浙江大学计算机及软件工程研究生机试真题)
- Javascript模块化编程学习(二):AMD规范和require.js的用法
- magento 开发 -- 深入理解Magento第四章 – 模型和ORM基础
- LeetCode: Sort Colors [075]
- C++开发必看 四种强制类型转换的总结
- three.js入门4——2014.5.27让场景动起来
- [数据结构] 二叉树的建立及其基本操作