免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3575 | 回复: 4

[C++] string类型截取数据 [复制链接]

论坛徽章:
0
发表于 2013-11-26 20:56 |显示全部楼层
本帖最后由 wuzen007 于 2013-11-26 21:02 编辑

程序中有个string数据为
string datas="id:{...}|{...}|{...}";
省略号里还有数据这里省略不写,现在想要的是获取每个{}里的值包括 '{' 和 '}' 并用另一个list<string>保存,存在只有一个{}数据所以 '|' 可能会没有,问如何能快速的获取?

论坛徽章:
0
发表于 2013-11-26 21:56 |显示全部楼层
  1. list<string> res;
  2. boost::algorithm::split(res, datas.substr(3), boost::is_any_of("|"), boost::algorithm::token_compress_on);
复制代码
有点限制,要保证{}里面没有包含 |字符

论坛徽章:
0
发表于 2013-11-26 22:01 |显示全部楼层
本帖最后由 wuzen007 于 2013-11-26 22:02 编辑

可以保证{}里面没有|,但可以用C++常用方法不要boost库么
回复 2# icosagon


   

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
发表于 2013-11-26 22:25 |显示全部楼层
去掉id: substr(3);
split('|‘);
trim("{}");

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2013-11-27 09:08 |显示全部楼层
我写了个示范,你再改改
用了两种方法分割,你选择其中一个就行了
  1. #include <iostream>
  2. #include <sstream>
  3. #include <list>
  4. #include <string>
  5. #include <cstring>
  6. using namespace std;

  7. // 使用C++的stringstream配合getline进行分割
  8. bool foo_cpp( const char* data, std::string& id, std::list<std::string>& subs )
  9. {
  10.     subs.clear();

  11.     istringstream is( data );
  12.    
  13.     getline( is, id, ':' );
  14.     if( !is ) return false;

  15.     for( string sub; getline(is,sub,'|'); )
  16.     {
  17.         if( sub.size()<2 || sub[0]!='{' || sub[sub.size()-1]!='}' )
  18.             return false;

  19.         subs.push_back( sub );
  20.     }

  21.     return true;
  22. }

  23. // 使用C的strchr进行分割
  24. bool foo_c( const char* data, std::string& id, std::list<std::string>& subs )
  25. {
  26.     subs.clear();

  27.     const char* p = strchr( data, ':' );
  28.     if( !p )
  29.         return false;
  30.     id = string( data, p-data );

  31.     const char* p1 = p+1;
  32.     for( const char* p2; (p2=strchr(p1,'|'))!=NULL; p1=p2+1 )
  33.     {
  34.         if( *p1!='{' || p2[-1]!='}' )
  35.             return false;

  36.         subs.push_back( string(p1,p2-p1) );
  37.     }
  38.     if( *p1 )
  39.     {
  40.         if( *p1!='{' || p1[strlen(p1)-1]!='}' )
  41.             return false;

  42.         subs.push_back( p1 );
  43.     }

  44.     return true;
  45. }

  46. // 测试函数
  47. void test( bool (&foo)(const char* data, std::string& id, std::list<std::string>& subs), const char* data )
  48. {
  49.     string id;
  50.     list<string> subs;
  51.     bool r = foo( data, id, subs );

  52.     if( !r )
  53.     {
  54.         cerr << "Error!!!\n";
  55.         return;
  56.     }

  57.     cout << "<SIZE=" << subs.size() << "> ";
  58.     cout << id << (subs.empty()?":":"");
  59.     for( list<string>::const_iterator itor=subs.begin(); itor!=subs.end(); ++itor )
  60.         cout << (itor==subs.begin()?':':'|') << *itor;
  61.     cout << endl;
  62. }

  63. int main()
  64. {
  65.     test( foo_cpp, "id000:{ABC}|{}|{BCD}" );
  66.     test( foo_cpp, "id001:{ABC}" );
  67.     test( foo_cpp, "id002:" );

  68.     test( foo_c, "id000:{ABC}|{}|{BCD}" );
  69.     test( foo_c, "id001:{ABC}" );
  70.     test( foo_c, "id002:" );

  71.     return 0;
  72. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP