深圳网站制作功能,辽阳网站设计,网站底部怎么做,网站后台生成htmlFrom#xff1a;http://www.jb51.net/article/55954.htm 1. 用strtok函数进行字符串分割 原型#xff1a; char *strtok(char *str, const char *delim); 功能#xff1a; 分解字符串为一组字符串。 参数说明#xff1a;str为要分解的字符串#xff0c;delim为…Fromhttp://www.jb51.net/article/55954.htm 1. 用strtok函数进行字符串分割 原型 char *strtok(char *str, const char *delim); 功能 分解字符串为一组字符串。 参数说明str为要分解的字符串delim为分隔符字符串。 返回值 从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。 其它 strtok函数线程不安全可以使用strtok_r替代。
代码示例
//借助strtok实现split
#include string.h
#include stdio.hint main()
{char chArry[] one two three,four * five;const char *d ,*;char *p;p strtok(s,d);while(p ! NULL){printf(%s\n, p);pstrtok(NULL, d);}return 0;
} 2. 用STL进行字符串的分割 涉及到string类的两个函数find和substr
1、find函数 原型 size_t find ( const string str, size_t pos 0 ) const; 功能 查找子字符串第一次出现的位置。 参数说明str为子字符串pos为初始查找位置。 返回值 找到的话返回第一次出现的位置否则返回string::npos 2、substr函数 原型 string substr ( size_t pos 0, size_t n npos ) const; 功能 获得子字符串。 参数说明pos为起始位置默认为0n为结束位置默认为npos 返回值 子字符串 示例代码
#include iostream
#include string
#include vector
//字符串分割函数
std::vectorstd::string split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vectorstd::string result;
strpattern;//扩展字符串以方便操作
int sizestr.size();
for(int i0; isize; i)
{
posstr.find(pattern,i);
if(possize)
{
std::string sstr.substr(i,pos-i);
result.push_back(s);
ipospattern.size()-1;
}
}
return result;
}
int main()
{
std::string str;
std::coutPlease input str:std::endl;
//std::cinstr;
getline(std::cin,str);
std::string pattern;
std::coutPlease input pattern:std::endl;
//std::cinpattern;
getline(std::cin,pattern);//用于获取含空格的字符串
std::vectorstd::string resultsplit(str,pattern);
std::coutThe result:std::endl;
for(int i0; iresult.size(); i)
{
std::coutresult[i]std::endl;
}
std::cin.get();
std::cin.get();
return 0;
} find_first_not_of
#includestring
#includevector
#includeiostream
using namespace std;void Tokenize(const string str, vectorstring tokens, const string delimiters)
{// Skip delimiters at beginning.string::size_type lastPos str.find_first_not_of(delimiters, 0);// Find first non-delimiter.string::size_type pos str.find_first_of(delimiters, lastPos);while (string::npos ! pos || string::npos ! lastPos){// Found a token, add it to the vector.tokens.push_back(str.substr(lastPos, pos - lastPos));// Skip delimiters. Note the not_oflastPos str.find_first_not_of(delimiters, pos);// Find next non-delimiterpos str.find_first_of(delimiters, lastPos);}
}
int main(int argc, char *argv[])
{string str(aaabbbcccddd);vectorstringtokens;Tokenize(str, tokens, );for( int i 0; i tokens.size() ; i ){cout tokens[i] endl;}return 0;
} 3. 用Boost进行字符串的分割 用boost库的正则表达式实现字符串分割
示例代码
#include iostream
#include cassert
#include vector
#include string
#include boost/regex.hppstd::vectorstd::string split(std::string str,std::string s)
{boost::regex reg(s.c_str());std::vectorstd::string vec;boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);boost::sregex_token_iterator end;while(it!end){vec.push_back(*it);}return vec;
}
int main()
{std::string str,s;strsss/ddd/ggg/hh;s/;std::vectorstd::string vecsplit(str,s);for(int i0,sizevec.size();isize;i){std::coutvec[i]std::endl;}std::cin.get();std::cin.get();return 0;
} boost里面有自带的split的函数如果用boost的话还是直接用split的好
示例代码
#include iostream
#include string
#include vector
#include boost/algorithm/string/classification.hpp
#include boost/algorithm/string/split.hppusing namespace std;int main()
{string s sss/ddd,ggg;vectorstring vStr;boost::split( vStr, s, boost::is_any_of( ,/ ), boost::token_compress_on );for( vectorstring::iterator it vStr.begin(); it ! vStr.end(); it )cout *it endl;return 0;
}