字符串处理,取出按指定分割符号(非空格)进行字符串切割

时间:2023-01-03 11:20:34
void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
   // return elems;
}

 

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <sstream>
#include<vector>
using namespace std;

void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
   // return elems;
}
int _tmain(int argc, _TCHAR* argv[]) { vector<string> a; string s="2018-08-15 05:46:47+0800"; split(s,':',a); cout<<a.size()<<endl; for(int i=0;i<a.size();i++){ cout<<a[i]<<endl; } }

字符串处理,取出按指定分割符号(非空格)进行字符串切割

 

void SplitString(const string& s, vector<short>& v, const string& c)
{
    string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while(string::npos != pos2)
    {
        v.push_back(str2num(s.substr(pos1, pos2-pos1)));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != s.length())
        v.push_back(str2num(s.substr(pos1)));
}