怎么得到 string str2={"qwe"},string str3={"bbb"},string str4={"333"}.
24 个解决方案
#1
JAVA 有现成API
C++貌似的自己实现
C++貌似的自己实现
#2
1. boost::algorithm::split
http://www.boost.org/doc/libs/1_52_0/doc/html/string_algo/usage.html
2. std::stringstream + std::getline
stringstream ss(str);
while(getline(ss, str, ' ')){ ... }
http://www.boost.org/doc/libs/1_52_0/doc/html/string_algo/usage.html
2. std::stringstream + std::getline
stringstream ss(str);
while(getline(ss, str, ' ')){ ... }
#3
可以用string的find和substr
或者字符串流处理:
或者字符串流处理:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void main()
{
string str1 = "qwe bbb 333";
string str2,str3,str4;
istringstream is(str1);
is>>str2>>str3>>str4;
cout<<str2<<","<<str3<<","<<str4<<endl;
}
#4
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1=" qwe bbb 333 ";
int iBegin = 0;
string::size_type iLatter = 0;
string::size_type iFormer = string::npos;
while (1)
{
iLatter = str1.find_first_not_of(' ', iLatter);
if (string::npos == iLatter)
{
break;
}
iFormer = str1.find_first_of(' ', iLatter + 1);
if (string::npos == iFormer)
{
iFormer = str1.length();
}
// str2, str3, str4
string strNew(str1, iLatter, iFormer - iLatter);
cout << strNew << endl;
iLatter = iFormer + 1;
}
getchar();
}
#5
他们写的都很好。。不过我用的是笨办法。。。
string str1="qwe bbb 333";
int index = str1.find(" ");
string str2 = str1.substr(0,index);
string str4= str1.substr(index);
index = str4.rfind(" ");
string str3 = str4.substr(index);
str4 = str4.substr(0,index);
还有点小瑕疵。。。
string str1="qwe bbb 333";
int index = str1.find(" ");
string str2 = str1.substr(0,index);
string str4= str1.substr(index);
index = str4.rfind(" ");
string str3 = str4.substr(index);
str4 = str4.substr(0,index);
还有点小瑕疵。。。
#6
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str="abc def ghi",word;
stringstream ss(str);
while(ss >> word)cout << word << endl;
ss.close();
return 0;
}
#7
SORRY,去掉ss.close()
#8
C可以用strtok函数
#9
boost str_algo split
#10
这是我自己实现的一个split函数。
template<class charT, class traits, class allocator>
vector<basic_string<charT, traits, allocator> >
inline split(const basic_string<charT, traits, allocator>& source,
const basic_string<charT, traits, allocator>& delimiter)
{
vector<basic_string<charT, traits, allocator> > result;
basic_string<charT, traits, allocator> input = source;
basic_string<charT, traits, allocator>::size_type index = input.find_first_of(delimiter);
while(index != basic_string<charT, traits, allocator>::npos)
{
basic_string<charT, traits, allocator> substr = input.substr(0, index);
input.erase(0, index + delimiter.size());
result.push_back(substr);
index = input.find_first_of(delimiter);
}
result.push_back(input);
return result;
}
template<class charT, class traits, class allocator>
vector<basic_string<charT, traits, allocator> >
inline split(const basic_string<charT, traits, allocator>& source,
const basic_string<charT, traits, allocator>& delimiter)
{
vector<basic_string<charT, traits, allocator> > result;
basic_string<charT, traits, allocator> input = source;
basic_string<charT, traits, allocator>::size_type index = input.find_first_of(delimiter);
while(index != basic_string<charT, traits, allocator>::npos)
{
basic_string<charT, traits, allocator> substr = input.substr(0, index);
input.erase(0, index + delimiter.size());
result.push_back(substr);
index = input.find_first_of(delimiter);
}
result.push_back(input);
return result;
}
#11
2楼和8楼的方法是比较简单的,8楼的具体例子可看http://baike.baidu.com/view/1028553.htm
#12
个人还是比较喜欢2楼的风格,简单明了
#13
strtok out了,现在纯C都改用strsep了
转这个文章
http://blog.csdn.net/baggiochen10/article/details/5900934
#14
最笨的办法是用标准库的stringstream,顶楼上的.
#15
这是我自己实现的一个扩展strtok函数。
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//连续出现时视为中间夹空token
const char *control2 //连续出现时视为中间无空token
)
{
unsigned char *str;
const unsigned char *ctrl1 = (const unsigned char *)control1;
const unsigned char *ctrl2 = (const unsigned char *)control2;
unsigned char map1[32],map2[32];
static char *nextoken;
static char flag=0;
unsigned char c;
int L;
memset(map1,0,32);
memset(map2,0,32);
do {
map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));
} while (*ctrl1++);
do {
map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));
} while (*ctrl2++);
if (string) {
if (control2[0]) {
L=strlen(string);
while (1) {
c=string[L-1];
if (map2[c >> 3] & (1 << (c & 7))) {
L--;
string[L]=0;
} else break;
}
}
if (control1[0]) {
L=strlen(string);
c=string[L-1];
if (map1[c >> 3] & (1 << (c & 7))) {
string[L]=control1[0];
string[L+1]=0;
}
}
str=(unsigned char *)string;
}
else str=(unsigned char *)nextoken;
string=(char *)str;
while (1) {
if (0==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
string++;
str++;
} else {
flag=1;
str++;
}
} else if (1==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=0;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=2;
break;
} else str++;
} else {//2==flag
if (!*str) return NULL;
if (map1[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
flag=0;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
} else {
string=(char *)str;
str++;
flag=1;
}
}
}
nextoken=(char *)str;
if (string==(char *)str) return NULL;
else return string;
}
void main()
{
strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");
strcpy(seps1,",\n");strcpy(seps2," \t");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");
strcpy(seps1,"|");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"");strcpy(seps2,"|");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"|");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,a,,b,,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,,");
strcpy(seps1,",");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
}
//
//[A string of ,,tokens
//
//and some more tokens,]
//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234| LIYI|China | 010 |201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,
#16
/**
* @file split.cpp
* @brief
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *split(const string &str, const string &sep)
{
vector<string> *svec = new vector<string>;
string::size_type beg = 0, end = 0;
beg = str.find_first_not_of(sep, end);
while (beg != string::npos) {
end = str.find_first_of(sep, beg);
if (end == string::npos) {
svec->push_back(string(str, beg));
break;
} else {
svec->push_back(string(str, beg, end - beg));
beg = str.find_first_not_of(sep, end);
}
}
return svec;
}
int main(int argc, char *argv[])
{
string s("qwe bbb 333");
vector<string> *svec;
vector<string>::const_iterator iter;
svec = split(s, " ");
for (iter = svec->begin(); iter != svec->end(); ++iter)
cout << *iter << endl;
delete svec;
return 0;
}
#17
万能的 sscanf 函数居然都让你给忽略了:
char *source = "aaa bbb ccc";
char a1[10], a2[10], a3[10];
sscanf(source, "%[^ ] %[^ ] %s", a1, a2, a3);
#18
请判断万能的sscanf函数的返回值。
#19
我只是提醒他有这个函数,没给他完整的代码。
#20
围观楼主,同时向各位大神学习
#21
向各位学习,顺便献上一份很简单的实做品
如果你们允许使用boost的话
谢谢版主的代码,收益良多,但我觉得回传值的部分应该可以更好一点
c++11中不会产生任何多余的copy,如果编译器有支援rvalue reference的话
一点意见,有错请鞭
如果你们允许使用boost的话
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
using namespace std;
void split_string()
{
string const s = "qwe bbb 333";
vector<string> rs;
boost::split( rs, s, boost::is_any_of( " " ), boost::token_compress_on );
for(auto const &data : rs){ //range based for loop, c++11才有
cout << data << endl;
}
cout << rs.size() << endl;
}
谢谢版主的代码,收益良多,但我觉得回传值的部分应该可以更好一点
vector<string> split(const string &str, const string &sep)
{
vector<string> svec;
//......
return svec;
}
c++11中不会产生任何多余的copy,如果编译器有支援rvalue reference的话
一点意见,有错请鞭
#22
前段时间刚写过这个代码,每个空格之间的字符赋值给一个Edit控件,以"#"键结束,你参照这改一下吧~
int len;
len=m_str.GetLength();
char ch;
CString str_num;
if (len)
{
for (int i=0;i<len;i++)
{
ch=m_str.GetAt(i);
if (ch==' ')//遇到空格则将以前到#的所有内容输出
{
switch(k)
{
case 0:m_edit1=str_num;break;
case 1:m_edit2=str_num;break;
default:
MessageBox(_T("现在仅支持两个参数输入!"));
k=0;
m_str.Empty();
EditInitialize();//控件显示初始化
dap_mark=TRUE;
}
if (dap_mark)
{
break;
}
k++;
UpdateData(FALSE);
str_num.Empty();
//break;
}
else if (ch=='#')//#作为结束符号
{
str_num+=ch;
str_num.Empty();
k=0;
m_str.Empty();
}
else
{
str_num+=ch;
continue;
}
}
}
k=0;//空格计数重新置零,针对分多次输入的累加情况
int len;
len=m_str.GetLength();
char ch;
CString str_num;
if (len)
{
for (int i=0;i<len;i++)
{
ch=m_str.GetAt(i);
if (ch==' ')//遇到空格则将以前到#的所有内容输出
{
switch(k)
{
case 0:m_edit1=str_num;break;
case 1:m_edit2=str_num;break;
default:
MessageBox(_T("现在仅支持两个参数输入!"));
k=0;
m_str.Empty();
EditInitialize();//控件显示初始化
dap_mark=TRUE;
}
if (dap_mark)
{
break;
}
k++;
UpdateData(FALSE);
str_num.Empty();
//break;
}
else if (ch=='#')//#作为结束符号
{
str_num+=ch;
str_num.Empty();
k=0;
m_str.Empty();
}
else
{
str_num+=ch;
continue;
}
}
}
k=0;//空格计数重新置零,针对分多次输入的累加情况
#23
C++11支持正则表达式了
#24
void change()
{
string a,b,c;
cout<<"input three words\n"<<endl;
cin>>a>>b>>c;
cout<<c<<" "<<b<<" "<<a<<endl;
}
{
string a,b,c;
cout<<"input three words\n"<<endl;
cin>>a>>b>>c;
cout<<c<<" "<<b<<" "<<a<<endl;
}
#1
JAVA 有现成API
C++貌似的自己实现
C++貌似的自己实现
#2
1. boost::algorithm::split
http://www.boost.org/doc/libs/1_52_0/doc/html/string_algo/usage.html
2. std::stringstream + std::getline
stringstream ss(str);
while(getline(ss, str, ' ')){ ... }
http://www.boost.org/doc/libs/1_52_0/doc/html/string_algo/usage.html
2. std::stringstream + std::getline
stringstream ss(str);
while(getline(ss, str, ' ')){ ... }
#3
可以用string的find和substr
或者字符串流处理:
或者字符串流处理:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void main()
{
string str1 = "qwe bbb 333";
string str2,str3,str4;
istringstream is(str1);
is>>str2>>str3>>str4;
cout<<str2<<","<<str3<<","<<str4<<endl;
}
#4
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1=" qwe bbb 333 ";
int iBegin = 0;
string::size_type iLatter = 0;
string::size_type iFormer = string::npos;
while (1)
{
iLatter = str1.find_first_not_of(' ', iLatter);
if (string::npos == iLatter)
{
break;
}
iFormer = str1.find_first_of(' ', iLatter + 1);
if (string::npos == iFormer)
{
iFormer = str1.length();
}
// str2, str3, str4
string strNew(str1, iLatter, iFormer - iLatter);
cout << strNew << endl;
iLatter = iFormer + 1;
}
getchar();
}
#5
他们写的都很好。。不过我用的是笨办法。。。
string str1="qwe bbb 333";
int index = str1.find(" ");
string str2 = str1.substr(0,index);
string str4= str1.substr(index);
index = str4.rfind(" ");
string str3 = str4.substr(index);
str4 = str4.substr(0,index);
还有点小瑕疵。。。
string str1="qwe bbb 333";
int index = str1.find(" ");
string str2 = str1.substr(0,index);
string str4= str1.substr(index);
index = str4.rfind(" ");
string str3 = str4.substr(index);
str4 = str4.substr(0,index);
还有点小瑕疵。。。
#6
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str="abc def ghi",word;
stringstream ss(str);
while(ss >> word)cout << word << endl;
ss.close();
return 0;
}
#7
SORRY,去掉ss.close()
#8
C可以用strtok函数
#9
boost str_algo split
#10
这是我自己实现的一个split函数。
template<class charT, class traits, class allocator>
vector<basic_string<charT, traits, allocator> >
inline split(const basic_string<charT, traits, allocator>& source,
const basic_string<charT, traits, allocator>& delimiter)
{
vector<basic_string<charT, traits, allocator> > result;
basic_string<charT, traits, allocator> input = source;
basic_string<charT, traits, allocator>::size_type index = input.find_first_of(delimiter);
while(index != basic_string<charT, traits, allocator>::npos)
{
basic_string<charT, traits, allocator> substr = input.substr(0, index);
input.erase(0, index + delimiter.size());
result.push_back(substr);
index = input.find_first_of(delimiter);
}
result.push_back(input);
return result;
}
template<class charT, class traits, class allocator>
vector<basic_string<charT, traits, allocator> >
inline split(const basic_string<charT, traits, allocator>& source,
const basic_string<charT, traits, allocator>& delimiter)
{
vector<basic_string<charT, traits, allocator> > result;
basic_string<charT, traits, allocator> input = source;
basic_string<charT, traits, allocator>::size_type index = input.find_first_of(delimiter);
while(index != basic_string<charT, traits, allocator>::npos)
{
basic_string<charT, traits, allocator> substr = input.substr(0, index);
input.erase(0, index + delimiter.size());
result.push_back(substr);
index = input.find_first_of(delimiter);
}
result.push_back(input);
return result;
}
#11
2楼和8楼的方法是比较简单的,8楼的具体例子可看http://baike.baidu.com/view/1028553.htm
#12
个人还是比较喜欢2楼的风格,简单明了
#13
strtok out了,现在纯C都改用strsep了
转这个文章
http://blog.csdn.net/baggiochen10/article/details/5900934
#14
最笨的办法是用标准库的stringstream,顶楼上的.
#15
这是我自己实现的一个扩展strtok函数。
#include <stdio.h>
#include <string.h>
char string[80];
char seps1[3];
char seps2[3];
char *token;
char *zzstrtok (
char *string,
const char *control1,//连续出现时视为中间夹空token
const char *control2 //连续出现时视为中间无空token
)
{
unsigned char *str;
const unsigned char *ctrl1 = (const unsigned char *)control1;
const unsigned char *ctrl2 = (const unsigned char *)control2;
unsigned char map1[32],map2[32];
static char *nextoken;
static char flag=0;
unsigned char c;
int L;
memset(map1,0,32);
memset(map2,0,32);
do {
map1[*ctrl1 >> 3] |= (1 << (*ctrl1 & 7));
} while (*ctrl1++);
do {
map2[*ctrl2 >> 3] |= (1 << (*ctrl2 & 7));
} while (*ctrl2++);
if (string) {
if (control2[0]) {
L=strlen(string);
while (1) {
c=string[L-1];
if (map2[c >> 3] & (1 << (c & 7))) {
L--;
string[L]=0;
} else break;
}
}
if (control1[0]) {
L=strlen(string);
c=string[L-1];
if (map1[c >> 3] & (1 << (c & 7))) {
string[L]=control1[0];
string[L+1]=0;
}
}
str=(unsigned char *)string;
}
else str=(unsigned char *)nextoken;
string=(char *)str;
while (1) {
if (0==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
string++;
str++;
} else {
flag=1;
str++;
}
} else if (1==flag) {
if (!*str) break;
if (map1[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=0;
break;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
*str=0;
str++;
flag=2;
break;
} else str++;
} else {//2==flag
if (!*str) return NULL;
if (map1[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
flag=0;
} else if (map2[*str >> 3] & (1 << (*str & 7))) {
str++;
string=(char *)str;
} else {
string=(char *)str;
str++;
flag=1;
}
}
}
nextoken=(char *)str;
if (string==(char *)str) return NULL;
else return string;
}
void main()
{
strcpy(string,"A \tstring\t\tof ,,tokens\n\nand some more tokens, ");
strcpy(seps1,",\n");strcpy(seps2," \t");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234| LIYI|China | 010 |201110260000|OK");
strcpy(seps1,"|");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"");strcpy(seps2,"|");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"1234|LIYI||010|201110260000|OK");
strcpy(seps1,"|");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,"a,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",a,,b");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,a,,b,,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,");
strcpy(seps1,",");strcpy(seps2,"");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
strcpy(string,",,,");
strcpy(seps1,",");strcpy(seps2," ");
printf("\n[%s]\nTokens:\n",string);
token=zzstrtok(string,seps1,seps2);
while (token!=NULL) {
printf(" <%s>",token);
token=zzstrtok(NULL,seps1,seps2);
}
}
//
//[A string of ,,tokens
//
//and some more tokens,]
//Tokens:
// <A>, <string>, <of>, <>, <tokens>, <>, <and>, <some>, <more>, <tokens>, <>,
//[1234| LIYI|China | 010 |201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <China>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <010>, <201110260000>, <OK>,
//[1234|LIYI||010|201110260000|OK]
//Tokens:
// <1234>, <LIYI>, <>, <010>, <201110260000>, <OK>,
//[a]
//Tokens:
// <a>,
//[a,b]
//Tokens:
// <a>, <b>,
//[a,,b]
//Tokens:
// <a>, <>, <b>,
//[,a]
//Tokens:
// <>, <a>,
//[a,]
//Tokens:
// <a>, <>,
//[,a,,b]
//Tokens:
// <>, <a>, <>, <b>,
//[,,a,,b,,]
//Tokens:
// <>, <>, <a>, <>, <b>, <>, <>,
//[,]
//Tokens:
// <>, <>,
//[,,]
//Tokens:
// <>, <>, <>,
//[,,,]
//Tokens:
// <>, <>, <>, <>,
#16
/**
* @file split.cpp
* @brief
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *split(const string &str, const string &sep)
{
vector<string> *svec = new vector<string>;
string::size_type beg = 0, end = 0;
beg = str.find_first_not_of(sep, end);
while (beg != string::npos) {
end = str.find_first_of(sep, beg);
if (end == string::npos) {
svec->push_back(string(str, beg));
break;
} else {
svec->push_back(string(str, beg, end - beg));
beg = str.find_first_not_of(sep, end);
}
}
return svec;
}
int main(int argc, char *argv[])
{
string s("qwe bbb 333");
vector<string> *svec;
vector<string>::const_iterator iter;
svec = split(s, " ");
for (iter = svec->begin(); iter != svec->end(); ++iter)
cout << *iter << endl;
delete svec;
return 0;
}
#17
万能的 sscanf 函数居然都让你给忽略了:
char *source = "aaa bbb ccc";
char a1[10], a2[10], a3[10];
sscanf(source, "%[^ ] %[^ ] %s", a1, a2, a3);
#18
请判断万能的sscanf函数的返回值。
#19
我只是提醒他有这个函数,没给他完整的代码。
#20
围观楼主,同时向各位大神学习
#21
向各位学习,顺便献上一份很简单的实做品
如果你们允许使用boost的话
谢谢版主的代码,收益良多,但我觉得回传值的部分应该可以更好一点
c++11中不会产生任何多余的copy,如果编译器有支援rvalue reference的话
一点意见,有错请鞭
如果你们允许使用boost的话
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
using namespace std;
void split_string()
{
string const s = "qwe bbb 333";
vector<string> rs;
boost::split( rs, s, boost::is_any_of( " " ), boost::token_compress_on );
for(auto const &data : rs){ //range based for loop, c++11才有
cout << data << endl;
}
cout << rs.size() << endl;
}
谢谢版主的代码,收益良多,但我觉得回传值的部分应该可以更好一点
vector<string> split(const string &str, const string &sep)
{
vector<string> svec;
//......
return svec;
}
c++11中不会产生任何多余的copy,如果编译器有支援rvalue reference的话
一点意见,有错请鞭
#22
前段时间刚写过这个代码,每个空格之间的字符赋值给一个Edit控件,以"#"键结束,你参照这改一下吧~
int len;
len=m_str.GetLength();
char ch;
CString str_num;
if (len)
{
for (int i=0;i<len;i++)
{
ch=m_str.GetAt(i);
if (ch==' ')//遇到空格则将以前到#的所有内容输出
{
switch(k)
{
case 0:m_edit1=str_num;break;
case 1:m_edit2=str_num;break;
default:
MessageBox(_T("现在仅支持两个参数输入!"));
k=0;
m_str.Empty();
EditInitialize();//控件显示初始化
dap_mark=TRUE;
}
if (dap_mark)
{
break;
}
k++;
UpdateData(FALSE);
str_num.Empty();
//break;
}
else if (ch=='#')//#作为结束符号
{
str_num+=ch;
str_num.Empty();
k=0;
m_str.Empty();
}
else
{
str_num+=ch;
continue;
}
}
}
k=0;//空格计数重新置零,针对分多次输入的累加情况
int len;
len=m_str.GetLength();
char ch;
CString str_num;
if (len)
{
for (int i=0;i<len;i++)
{
ch=m_str.GetAt(i);
if (ch==' ')//遇到空格则将以前到#的所有内容输出
{
switch(k)
{
case 0:m_edit1=str_num;break;
case 1:m_edit2=str_num;break;
default:
MessageBox(_T("现在仅支持两个参数输入!"));
k=0;
m_str.Empty();
EditInitialize();//控件显示初始化
dap_mark=TRUE;
}
if (dap_mark)
{
break;
}
k++;
UpdateData(FALSE);
str_num.Empty();
//break;
}
else if (ch=='#')//#作为结束符号
{
str_num+=ch;
str_num.Empty();
k=0;
m_str.Empty();
}
else
{
str_num+=ch;
continue;
}
}
}
k=0;//空格计数重新置零,针对分多次输入的累加情况
#23
C++11支持正则表达式了
#24
void change()
{
string a,b,c;
cout<<"input three words\n"<<endl;
cin>>a>>b>>c;
cout<<c<<" "<<b<<" "<<a<<endl;
}
{
string a,b,c;
cout<<"input three words\n"<<endl;
cin>>a>>b>>c;
cout<<c<<" "<<b<<" "<<a<<endl;
}