答案 0 :(得分:18)
这是我使用的perl风格的分割功能:
void split(const string& str, const string& delimiters , vector<string>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
答案 1 :(得分:11)
没有内置的方法可以在C ++中拆分字符串,但是boost提供string algo库来执行所有类型的字符串操作,包括字符串splitting。
答案 2 :(得分:10)
Yup,stringstream。
std::istringstream oss(std::string("This is a test string"));
std::string word;
while(oss >> word) {
std::cout << "[" << word << "] ";
}
答案 3 :(得分:7)
STL字符串
你可以使用字符串迭代器来完成你的工作。
std::string str = "hello world";
std::string::const_iterator pos = std::find(string.begin(), string.end(), ' '); // Split at ' '.
std::string left(str.begin(), pos);
std::string right(pos + 1, str.end());
// Echoes "hello|world".
std::cout << left << "|" << right << std::endl;
答案 4 :(得分:3)
void split(string StringToSplit, string Separators)
{
size_t EndPart1 = StringToSplit.find_first_of(Separators)
string Part1 = StringToSplit.substr(0, EndPart1);
string Part2 = StringToSplit.substr(EndPart1 + 1);
}
答案 5 :(得分:1)
答案是否定的。你必须使用其中一个库函数来解析它们。
我使用的东西:
std::vector<std::string> parse(std::string l, char delim)
{
std::replace(l.begin(), l.end(), delim, ' ');
std::istringstream stm(l);
std::vector<std::string> tokens;
for (;;) {
std::string word;
if (!(stm >> word)) break;
tokens.push_back(word);
}
return tokens;
}
您还可以查看basic_streambuf<T>::underflow()
方法并编写过滤器。
答案 6 :(得分:0)
到底是什么......这是我的版本......
注意:拆分(“XZaaaXZ”,“XZ”)会给你3个字符串。其中2个字符串为空,如果IncludeEmptyStrings为false,将不会添加到theStringVector。
分隔符不集合中的任何元素,而是匹配该字符串。
inline void
StringSplit( vector<string> * theStringVector, /* Altered/returned value */
const string & theString,
const string & theDelimiter,
bool theIncludeEmptyStrings = false )
{
UASSERT( theStringVector, !=, (vector<string> *) NULL );
UASSERT( theDelimiter.size(), >, 0 );
size_t start = 0, end = 0, length = 0;
while ( end != string::npos )
{
end = theString.find( theDelimiter, start );
// If at end, use length=maxLength. Else use length=end-start.
length = (end == string::npos) ? string::npos : end - start;
if ( theIncludeEmptyStrings
|| ( ( length > 0 ) /* At end, end == length == string::npos */
&& ( start < theString.size() ) ) )
theStringVector -> push_back( theString.substr( start, length ) );
// If at end, use start=maxSize. Else use start=end+delimiter.
start = ( ( end > (string::npos - theDelimiter.size()) )
? string::npos : end + theDelimiter.size() );
}
}
inline vector<string>
StringSplit( const string & theString,
const string & theDelimiter,
bool theIncludeEmptyStrings = false )
{
vector<string> v;
StringSplit( & v, theString, theDelimiter, theIncludeEmptyStrings );
return v;
}
答案 7 :(得分:0)
这样做没有共同的方法。
我更喜欢boost::tokenizer,它只是标题,易于使用。
答案 8 :(得分:-2)
C字符串
只需在要分割的地方插入\0
即可。这与标准C函数一样内置。
此函数在char
分隔符的第一次出现时分割,返回第二个字符串。
char *split_string(char *str, char separator) {
char *second = strchr(str, separator);
if(second == NULL)
return NULL;
*second = '\0';
++second;
return second;
}
答案 9 :(得分:-2)
一个相当简单的方法是使用std :: string的c_str()方法来获取C样式的字符数组,然后使用strtok()来标记字符串。不像这里列出的其他一些解决方案那样雄辩,但它很容易且有效。