C++ String Split
来源:互联网 发布:java smtp邮件服务器 编辑:程序博客网 时间:2024/06/08 02:12
C++ String Split
#include <string>
#include <vector>
namespace Daniweb
{
using namespace std;
typedef string::size_type (string::*find_t)(const string& delim,
string::size_type offset) const;
/// <summary>
/// Splits the string s on the given delimiter(s) and
/// returns a list of tokens without the delimiter(s)
/// </summary>
/// <param name=s>The string being split</param>
/// <param name=match>The delimiter(s) for splitting</param>
/// <param name=removeEmpty>Removes empty tokens from the list</param>
/// <param name=fullMatch>
/// True if the whole match string is a match, false
/// if any character in the match string is a match
/// </param>
/// <returns>A list of tokens</returns>
vector<string> Split(const string& s,
const string& match,
bool removeEmpty=false,
bool fullMatch=false)
{
vector<string> result; // return container for tokens
string::size_type start = 0, // starting position for searches
skip = 1; // positions to skip after a match
find_t pfind = &string::find_first_of; // search algorithm for matches
if (fullMatch)
{
// use the whole match string as a key
// instead of individual characters
// skip might be 0. see search loop comments
skip = match.length();
pfind = &string::find;
}
while (start != string::npos)
{
// get a complete range [start..end)
string::size_type end = (s.*pfind)(match, start);
// null strings always match in string::find, but
// a skip of 0 causes infinite loops. pretend that
// no tokens were found and extract the whole string
if (skip == 0) end = string::npos;
string token = s.substr(start, end - start);
if (!(removeEmpty && token.empty()))
{
// extract the token and add it to the result list
result.push_back(token);
}
// start the next range
if ((start = end) != string::npos) start += skip;
}
return result;
}
}
- split string in C
- C#String.Split (string[], StringSplitOptions)
- 4.C#:string.Split 和 string.Join
- C#string的Split和Join
- Split a C string on whitespace
- stringutils.split string.split
- String#split
- String split()
- String.split()
- String.split
- String split()
- String.split()
- String.split
- String split
- String .split
- string.split
- String.split
- String split()
- 生产订单更新
- silverlight超小游戏开发包SmallGameLib(四)—— SGL开源
- 如何以 JAVA call 一個現有的 dll 檔?【转】
- Oracle 默认密码及修改密码
- 详解spring2.0的scope
- C++ String Split
- TCP/IP选项解析(Java Socket)
- 一个类似Java String[] split(String regex)的VC++函数【】
- ioctl 网络操作
- MSChart控件的属性与属性对话框 【转】
- 优秀的Java程序员必须了解GC的工作原理(转)
- JS比较相似度
- 关于DOM4J的Namespace的问题(XPath)
- 利用BCGControlBar 实现状态条显示Progressbar 【转】