1、c++版本,第一个参数为待分割的字符串 , 第二个参数为分割字符串
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
std::vector<std::string> elems;
size_t pos = 0;
size_t len = s.length();
size_t delim_len = delim.length();
if (delim_len == 0) return elems;
while (pos < len)
{
int find_pos = s.find(delim, pos);
if (find_pos < 0)
{
elems.push_back(s.substr(pos, len - pos));
break;
}
elems.push_back(s.substr(pos, find_pos - pos));
pos = find_pos + delim_len;
}
return elems;
}
2、c语言版本
char *line = "0.5|0.6";
char seg[] = "|";
char *substr = strtok(line, seg);
float a;
while (substr != NULL)
{
a = atof(substr);
substr = strtok(NULL, seg);
printf("%f" , a);
}