i am trying to split a string with 2 delimiters '+' and '-' in C++ using a string find a delimiter...
我试图在C ++中使用字符串分割带有2个分隔符'+'和' - '的字符串,找到分隔符...
can anyone give me a go around...
任何人都可以给我一个...
Using
str.find(delimiter)
example :
a+b-c+d
Output Required: a b c d
需要输出:a b c d
Thanks in advance
提前致谢
3 个解决方案
#1
14
Using std::string::substr
and std::string::find
使用std :: string :: substr和std :: string :: find
std::vector<std::string> v ; //Use vector to add the words
std::size_t prev_pos = 0, pos;
while ((pos = str.find_first_of("+-", prev_pos)) != std::string::npos)
{
if (pos > prev_pos)
v.push_back(str.substr(prev_pos, pos-prev_pos));
prev_pos= pos+1;
}
if (prev_pos< str.length())
v.push_back(str.substr(prev_pos, std::string::npos));
Or if you use boost
it will be lot easier
或者,如果你使用boost,那将会容易得多
#include <boost/algorithm/string.hpp>
std::vector<std::string> v;
boost::split(v, line, boost::is_any_of("+-"));
#2
0
you can do this for variable delimiters as well
您也可以为变量分隔符执行此操作
void main void()
{
char stringToUpdate[100] , char delimeters[4];
/*
write code to assign strings and delimeter
*/
replaceDelimeters(stringToUpdate, delimeters, ' ');
}
void replaceDelimeters(char* myString, char* delimeters, char repChar)
{
for (int i = 0; delimeters[i] != '\0'; i++)
{
for(int j=0; stringtoUpdate[j] != '\0'; j++)
{
if(stringtoUpdate[j] == delimeters[i])
{
stringtoUpdate[j] = repChar;
}
}
}
}
#3
0
Use the function "char* strtok(char* src, const char* delimiters)" http://en.cppreference.com/w/cpp/string/byte/strtok
使用函数“char * strtok(char * src,const char * delimiters)”http://en.cppreference.com/w/cpp/string/byte/strtok
char* s = "a+b-c+d";
char* p = strtok(s, "+-");
while (p != NULL)
{
// do something with p
p = strtok (NULL, "+-");
}
#1
14
Using std::string::substr
and std::string::find
使用std :: string :: substr和std :: string :: find
std::vector<std::string> v ; //Use vector to add the words
std::size_t prev_pos = 0, pos;
while ((pos = str.find_first_of("+-", prev_pos)) != std::string::npos)
{
if (pos > prev_pos)
v.push_back(str.substr(prev_pos, pos-prev_pos));
prev_pos= pos+1;
}
if (prev_pos< str.length())
v.push_back(str.substr(prev_pos, std::string::npos));
Or if you use boost
it will be lot easier
或者,如果你使用boost,那将会容易得多
#include <boost/algorithm/string.hpp>
std::vector<std::string> v;
boost::split(v, line, boost::is_any_of("+-"));
#2
0
you can do this for variable delimiters as well
您也可以为变量分隔符执行此操作
void main void()
{
char stringToUpdate[100] , char delimeters[4];
/*
write code to assign strings and delimeter
*/
replaceDelimeters(stringToUpdate, delimeters, ' ');
}
void replaceDelimeters(char* myString, char* delimeters, char repChar)
{
for (int i = 0; delimeters[i] != '\0'; i++)
{
for(int j=0; stringtoUpdate[j] != '\0'; j++)
{
if(stringtoUpdate[j] == delimeters[i])
{
stringtoUpdate[j] = repChar;
}
}
}
}
#3
0
Use the function "char* strtok(char* src, const char* delimiters)" http://en.cppreference.com/w/cpp/string/byte/strtok
使用函数“char * strtok(char * src,const char * delimiters)”http://en.cppreference.com/w/cpp/string/byte/strtok
char* s = "a+b-c+d";
char* p = strtok(s, "+-");
while (p != NULL)
{
// do something with p
p = strtok (NULL, "+-");
}