免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: coolfire666
打印 上一主题 下一主题

解析字符串的问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-10-23 19:43 |只看该作者
原帖由 coolfire666 于 2007-10-23 17:30 发表
xB1ue兄 谢谢了
还有ls其他兄弟 学习了
4l说的我脸都红了 要发愤了...

贴个请教别人的code

#include
#include
#include
#include
using namespace std;

int main()
{
    string str = "ke ...


没有出错处理,好歹来点 try...catch 之类的

论坛徽章:
0
12 [报告]
发表于 2007-10-23 21:31 |只看该作者
原帖由 xB1ue 于 2007-10-23 19:43 发表


没有出错处理,好歹来点 try...catch 之类的


程序中并没有异常抛出,所以不需要 try...catch。注意使用 C++ 标准库时大部分情况都不抛出异常。

论坛徽章:
0
13 [报告]
发表于 2007-10-23 21:33 |只看该作者

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iterator>
  6. using namespace std;

  7. /*! From a string, extract all sub-strings separated by the delimiters
  8. * and append them to a vector container.
  9. */
  10. void split_string( const string& str, const string& delims,
  11.                    vector<string>& str_vector );

  12. int main()
  13. {
  14.   const string test_str = "kevin:pass@127.0.0.1:/var/path/kevin/demo.cpp";
  15.   vector<string> vs;        // used to save the extracted sub-strings.

  16.   // Get the sub-strings separated by ':' or '@'.
  17.   split_string( test_str, ":@", vs );

  18.   // Separate the path and file name.
  19.   string::size_type pos = vs.back().rfind('/');
  20.   vs.push_back( vs.back().substr( pos + 1 ) );
  21.   vs[vs.size() - 2].erase( pos );

  22.   // Output the extracted sub-strings.
  23.   cout << "Number of extracted sub-string: " << vs.size() << "\n\t";
  24.   copy( vs.begin(), vs.end(), ostream_iterator<string>(cout, "\n\t") );
  25. }

  26. void split_string( const string& str, const string& delims,
  27.                    vector<string>& str_vector )
  28. {
  29.   string::size_type pos_prev = 0;
  30.   string::size_type pos;
  31.   while ((pos = str.find_first_of( delims, pos_prev )) != string::npos) {
  32.     str_vector.push_back( str.substr(pos_prev, pos - pos_prev) );
  33.     pos_prev = pos + 1;
  34.   }
  35.   str_vector.push_back( str.substr(pos_prev) );
  36. }
复制代码

论坛徽章:
0
14 [报告]
发表于 2007-11-02 13:00 |只看该作者
原帖由 ivhb 于 2007-10-23 18:37 发表
不推荐使用这个
尽管看起来很简单

const char *s = "usr:pwd@ip:path/file";
char buf[5][10];

sscanf(s, "%[^:]:%[^@]@%[^:]:%[^/]/%s", buf[0], buf[1], buf[2], buf[3], buf[4]);

这个是高!!!!!!!!!!

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
15 [报告]
发表于 2007-11-02 13:33 |只看该作者
  1. char usr[50];
  2. char pwd[50];
  3. char ip[50];
  4. char path [256];
  5. char file[256];
  6. sscanf(src,"%49[^:]:%49[^@]@%49[^:]:%255[^/]/%255s",usr,pwd,ip,path,file);
复制代码

即可,
只是不知道你的path里是否含有/

论坛徽章:
0
16 [报告]
发表于 2007-11-02 13:48 |只看该作者
受教了啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP