How I do split in C++ ?
Ex:
test1;;test2;;test3
How do I take those and stick those in variables ?
In perl would be
my($var1,$var2,$var3)=split(/;;/, $input);
How do I do that in c++ ?
Printable View
How I do split in C++ ?
Ex:
test1;;test2;;test3
How do I take those and stick those in variables ?
In perl would be
my($var1,$var2,$var3)=split(/;;/, $input);
How do I do that in c++ ?
I never thought of it myself though I use it PHP, so I decided to write an implementation:
probably could be optimised though.Code:bool split(std::string sep, std::string str, std::vector<std::string> &array) {
int seplen = sep.length();
if (seplen < 1)
return false;
int len = str.length();
int find = 0, prev = 0;
for (;;) {
if (find = str.find(sep, find) == std::string::npos) {
array.push_back(str.substr(prev, len - prev));
return true;
}
else {
array.push_back(str.substr(prev, find - prev));
find += seplen;
prev = find;
}
}
return false;
}
The difference would be that we would be passing a vector of strings by reference, and then accessing the elements of the vector, rather than directly assigning the values to individual variables.
strtok()