« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9702220 建立时间:2004年12月20日 |

| |
[c++]c语言和c++切分字符串的问题 原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2005/11/23 16:11:44 |
c语言strtok#include <string.h>#include <stdio.h>
char string[] = "A string\tof ,,tokens\nand some more tokens";char seps[] = " ,\t\n";char *token;
void main( void ){ printf( "%s\n\nTokens:\n", string ); /* Establish string and get the first token: */ token = strtok( string, seps ); while( token != NULL ) { /* While there are tokens in "string" */ printf( " %s\n", token ); /* Get next token: */ token = strtok( NULL, seps ); }}// OutputA string of ,,tokensand some more tokens
Tokens: A string of tokens and some more tokens问题是 使用static数组,多线程有问题。标准c++的做法#include <string>#include <vector>#include <functional>#include <iostream>
using namespace std;
void split(const string& s, char c, vector<string>& v) { string::size_type i = 0; string::size_type j = s.find(c);
while (j != string::npos) { v.push_back(s.substr(i, j-i)); i = ++j; j = s.find(c, j);
if (j == string::npos) v.push_back(s.substr(i, s.length( ))); }}
int main( ) { vector<string> v; string s = "Account Name|Address 1|Address 2|City";
split(s, '|', v);
for (int i = 0; i < v.size( ); ++i) { cout << v[i] << '\n'; }}使用templatetemplate<typename T>void split(const basic_string<T>& s, T c, vector<basic_string<T> >& v) { basic_string<T>::size_type i = 0; basic_string<T>::size_type j = s.find(c);
while (j != basic_string<T>::npos) { v.push_back(s.substr(i, j-i)); i = ++j; j = s.find(c, j);
if (j == basic_string<T>::npos) v.push_back(s.substr(i, s.length( ))); }}
使用boost#include <iostream>#include <string>#include <list>#include <boost/algorithm/string.hpp>
using namespace std;using namespace boost;
int main( ) {
string s = "one,two,three,four"; list<string> results;
split(results, s, is_any_of(",")); // Note this is boost::split
for (list<string>::const_iterator p = results.begin( ); p != results.end( ); ++p) { cout << *p << endl; }}使用boost的正则表达式#include <iostream>#include <string>#include <boost/regex.hpp>
int main( ) {
std::string s = "who,lives:in-a,pineapple under the sea?";
boost::regex re(",|:|-|\\s+"); // Create the reg exp boost::sregex_token_iterator // Create an iterator using a p(s.begin( ), s.end( ), re, -1); // sequence and that reg exp boost::sregex_token_iterator end; // Create an end-of-reg-exp // marker while (p != end) std::cout << *p++ << '\n';}使用stl比较简单的切分#include <string>#include <iostream>
using namespace std;
// String tokenizer class.class StringTokenizer {
public:
StringTokenizer(const string& s, const char* delim = NULL) : str_(s), count_(-1), begin_(0), end_(0) {
if (!delim) delim_ = " \f\n\r\t\v"; //default to whitespace else delim_ = delim;
// Point to the first token begin_ = str_.find_first_not_of(delim_); end_ = str_.find_first_of(delim_, begin_); }
size_t countTokens( ) { if (count_ >= 0) // return if we've already counted return(count_);
string::size_type n = 0; string::size_type i = 0;
for (;;) { // advance to the first token if ((i = str_.find_first_not_of(delim_, i)) == string::npos) break; // advance to the next delimiter i = str_.find_first_of(delim_, i+1); n++; if (i == string::npos) break; } return (count_ = n); } bool hasMoreTokens( ) {return(begin_ != end_);} void nextToken(string& s) { if (begin_ != string::npos && end_ != string::npos) { s = str_.substr(begin_, end_-begin_); begin_ = str_.find_first_not_of(delim_, end_); end_ = str_.find_first_of(delim_, begin_); } else if (begin_ != string::npos && end_ == string::npos) { s = str_.substr(begin_, str_.length( )-begin_); begin_ = str_.find_first_not_of(delim_, end_); }
}
private: StringTokenizer( ) {}; string delim_; string str_; int count_; int begin_; int end_;};
int main( ) { string s = " razzle dazzle giddyup "; string tmp;
StringTokenizer st(s);
cout << "there are " << st.countTokens( ) << " tokens.\n"; while (st.hasMoreTokens( )) { st.nextToken(tmp); cout << "token = " << tmp << '\n'; }} |
|
|