假设我有一个std :: string“55 | 6999 | dkfdfd |”。它共同拥有3部分(每部分后面都有|)。目前我将字符串放到stringstream中,并使用getline来恢复它们。不过,我不知道是否有一个更简单的解决方案,不需要流。我只需要一个简单的方法来从该字符串中使用'|'作为delim,因为我认为流是过度的,我做错了。Get string from string(not from stringstream)
回答
如果允许提升,那么boost::split
将是一个选项。它可用于填充std::vector<std::string>
,其中将包括基于指定的分隔符(S)的领域从输入提取:
#include <vector>
#include <string>
using std::vector;
using std::string;
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
using boost::split;
using boost::is_any_of;
vector<string> fields;
split(fields, "55|6999|dkfdfd|", is_any_of("|"));
你可以使用std::strtok
来代替:
char *token = std::strtok(<yourstring>, "|");
< yourstring>必须是char*
型的,虽然;然后使用NULL
作为之后的那个,std::strtok
跟踪以前使用的字符串。
token = std::strtok(NULL, "|");
谢谢你,所以我是对的,有一个更好的解决方案。 – user1873947 2013-02-12 18:04:10
@ user1873947,注意'strtok'修改输入,所以使用字符串文字将是非法的,例如。 – hmjd 2013-02-12 18:06:01
你在说什么叫做“标记”字符串。这通常是你最好的选择。至于怎么做,请参阅“How do I tokenize a string in C++?”
boost :: tokenizer看起来不错。 – user1873947 2013-02-12 18:05:36
- 1. Magento date from string
- 2. date from string YYMMDDHHMMSS
- 3. date from string
- 4. 删除全部。 from string perl
- 5. Date from String with .medium Format
- 6. “url last”from url as string
- 7. Can not get slidesCount from $ ionicSlideBoxDelegate
- 8. string(from substr)转换为unsigned int
- 9. memorystream - stringstream,string,others?
- 10. Array to string conversion while ajax call from javascript
- 11. Cut <br/> -Tags from String end
- 12. if(String == null or String ==“”)not activate
- 13. becomeFirstResponder not displayed from viewDidLoad
- 14. webRequest do not work from different user
- 15. snmp-get value from pass is missing
- 16. Java not found from mozilla add
- 17. function fetch_images()from folder not api
- 18. mysql select * from table where not null
- 19. asp.net mvc 2 model not from db
- 20. img src code not render from javascript
- 21. Ionic 2:src from img not updating
- 22. mysql select * from not in a table
- 23. Get apache.commons.cli.CommandLine as String
- 24. Get boost from boost :: gregorian :: date_duration
- 25. Get RSS Url from RSS MEDIUM FEED
- 26. image not loading使用json from url
- 27. 查找并提取<a href> link from string in python
- 28. I get a error message from my vb6 program
- 29. MySQL SELECT x FROM WHERE NOT IN(SELECT x FROM b) - 意外的结果
- 30. String concatenation - valueOf or not
另一个不错的选择,谢谢。 – user1873947 2013-02-12 18:07:35