string find
来源:互联网 发布:编程游戏软件 编辑:程序博客网 时间:2024/06/06 13:10
#include <iostream>#include <vector>#include <string>#include <tchar.h>#include <string.h>using namespace std;/************************************************************************//* 查找一个字符在一个字符串中第n次出现的位置 first parameter str: source string second parameter c: find char third parameter n: Nst show return : index of c in str*//************************************************************************/int FindNstPosition(const char* str, char c, int n){ // ASSERT(str); if(!str || c == '\0') return -1; const char * p = str; int nIndex = 0; int nCount = 0; while(*p != '\0') { if(*p == c) { nCount++; if(nCount == n) return nIndex; else { nIndex++; p++; } } else { nIndex++; p++; } } return -1;}/************************************************************************//* 以指定的分割符分割字符串,并将分割后的字符串放到vector<string>中 src: source string separator : separator string dest : dest vector string*//************************************************************************/void mySplit(const string& src, const string& separator, vector<string>& dest){ string str = src; string substring = ""; string::size_type start = 0, nIndex = 0; do { nIndex = str.find_first_of(separator, start); if(nIndex != string::npos) { substring = str.substr(start, nIndex - start); dest.push_back(substring); start = str.find_first_not_of(separator, nIndex); if(start == string::npos) return; } } while (nIndex != string::npos); // the last token substring = str.substr(start); dest.push_back(substring);}int main(){ vector<string> vecStr; vecStr.push_back("test1&test2&test3&test4&test5&test6"); cout << vecStr[0] << endl; cout << "sizeof(vecStr[0]) = " << sizeof(vecStr[0]) << endl; cout << "strlen(vecStr[0]) = " << strlen(vecStr[0].c_str()) << endl; int nIndex = FindNstPosition(vecStr[0].c_str(), 'e', 5); cout << nIndex << endl; cout << endl; vector<string> strDest; string separator = "&"; mySplit(vecStr[0], separator, strDest); for(size_t i = 0; i < strDest.size(); i++) cout << strDest[i] << endl; system("pause"); return 0;}
阅读全文
0 0
- string find
- string find
- string find
- string.find()
- string.find() string.substr();
- string::find string::npos
- string::find 与 string::npos
- c++之string.find(string)
- [LzmTW].[String].[Find]
- find some string
- string find查找函数
- string copy find
- Python String find() Method
- C++ string.find()
- string.find()函数用法
- std::string find函数
- Python string find
- lua string.find
- Javaweb项目开发学习(一)——登录界面servlet
- hive实战---动态分区
- ORA-00257归档日志写满的解决方法 背景: 在前一篇博客中我们提到了如何启动或关闭oracle的归档(ARCHIVELOG)模式,在我成功设定数据库为归档模式以后, 第二天再次尝试连接数据库,
- 欧几里德算法 & 扩展欧几里德算法
- POJ3321(dfs序+树状数组)
- string find
- Mocha ES6学习记录
- 学习笔记1
- 算法之度度熊回家
- Spring MVC简单实例,含所有代码,Mark之后可运行
- tomcat端口号8080被占用,修改后还是占用问题。
- Prime Path
- unity shader 常用内置函数变量和语义
- 笨方法学Python 习题 12: 提示别人