字符串分割使用方法

来源:互联网 发布:淘宝客程序2.0 编辑:程序博客网 时间:2024/05/17 03:25
// splitstring.cpp : Defines the entry point for the console application.//#include "stdafx.h"#pragma warning(disable : 4786)#include <vector>#include <string>//字符串分割函数std::vector<std::string> split(std::string str,std::string pattern){    std::string::size_type pos;    std::vector<std::string> result;    str+=pattern;//扩展字符串以方便操作    int size=str.size();    for(int i=0; i<size; i++)    {        pos=str.find(pattern,i);        if(pos<size)        {            std::string s=str.substr(i,pos-i);            result.push_back(s);            i=pos+pattern.size()-1;        }    }    return result;}int main(int argc, char* argv[]){std::vector<std::string> szArry;szArry = split("good,asdf,asdf,as,dfas,df,asdf,as,dfas",",");std::vector<std::string>::iterator index;for (index = szArry.begin(); index != szArry.end(); index++){printf("%s\n",(*index).c_str() );}return 0;}


分割函数,是某个大侠写的,这里借用了一下!

vector<CString> split(CString str,CString pattern)  {  int pos;  vector<CString> result;  str+=pattern;//扩展字符串以方便操作  int size=str.GetLength();  for(int i=0; i<size; i++)  {  pos = str.Find(pattern,i);if(pos<size)  {  CString s=str.Mid(i,pos-i);  result.push_back(s);  i = pos + pattern.GetLength() - 1;}  }  return result;  }




0 0
原创粉丝点击