码迷,mamicode.com
首页 > 编程语言 > 详细

C++常用字符串分割方法

时间:2015-12-12 15:33:04      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

一、用strtok函数进行字符串分割

原型: char *strtok(char *str, const char *delim);

功能:分解字符串为一组字符串。

参数说明:str为要分解的字符串,delim为分隔符字符串。

返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。

其它:strtok函数线程不安全,可以使用strtok_r替代。

//借助strtok实现split
#include <string.h>
#include <stdio.h>
 
int main()
{
    char s[] = "Golden Global   View,disk * desk";
    const char *d = " ,*";
    char *p;
    p = strtok(s,d);
    while(p)
    {
        printf("%s\n",p);
        p=strtok(NULL,d);
    }
 
    return 0;
}

二、用STL进行字符串的分割

涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos

2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串

#include <iostream>
#include <string>
#include <vector>
 
//字符串分割函数
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()
{
  std::string str;
  std::cout<<"Please input str:"<<std::endl;
  //std::cin>>str;
  getline(std::cin,str);
  std::string pattern;
  std::cout<<"Please input pattern:"<<std::endl;
  //std::cin>>pattern;
  getline(std::cin,pattern);//用于获取含空格的字符串
  std::vector<std::string> result=split(str,pattern);
  std::cout<<"The result:"<<std::endl;
  for(int i=0; i<result.size(); i++)
  {
    std::cout<<result[i]<<std::endl;
  }
 
  std::cin.get();
  std::cin.get();
  return 0;
}

三、用Boost进行字符串的分割

用boost库的正则表达式实现字符串分割

#include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include "boost/regex.hpp"
 
std::vector<std::string> split(std::string str,std::string s)
{
    boost::regex reg(s.c_str());
    std::vector<std::string> vec;
    boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
    boost::sregex_token_iterator end;
    while(it!=end)
    {
        vec.push_back(*it++);
    }
    return vec;
}
int main()
{
    std::string str,s;
    str="sss/ddd/ggg/hh";
    s="/";
    std::vector<std::string> vec=split(str,s);
    for(int i=0,size=vec.size();i<size;i++)
    {
        std::cout<<vec[i]<<std::endl;
    }
    std::cin.get();
    std::cin.get();
    return 0;
}

boost里面有自带的split的函数,如果用boost的话,还是直接用split的好

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
  
using namespace std;
  
int main()
{
 string s = "sss/ddd,ggg";
 vector<string> vStr;
 boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );
 for( vector<string>::iterator it = vStr.begin(); it != vStr.end(); ++ it )
  cout << *it << endl;
 return 0;
}

使用Boost库

1)使用boost::split。根据字符来split,注意is_any_of的意思是集合中的任何一个,而不是equals的意思,那如何根据字符串split原字符串?请看2和3。

#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
    string str( "dfa&fda|dfdf");
    vector<string> tokens;
    boost::split(tokens, str, boost::is_any_of("&|"));   
    for (size_t i = 0; i < tokens.size(); ++ i)
    {
        cout << tokens[i] << endl;
    }
    return 0;
}

2) 使用boost::tokenize,注意 boost::char_separator<char>分隔符一样是集合的概念!但划分方法和boost::split不大一样,详看boost文档。

#include <iostream>
#include <vector>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
int main()
{
    string str("dfa||fda||dfdf");
    vector<string> tokens;
    boost::char_separator<char> sep("|");
    boost::tokenizer<boost::char_separator<char> > tok(str, sep);
    tokens.clear();
    std::copy(tok.begin(), tok.end(), std::back_inserter(tokens));   
    for (size_t i = 0; i < tokens.size(); ++ i)
    {
        cout << tokens[i] << endl;
    }
    return 0;
}

3)使用boost::regex

#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main()
{
    string str("dfa||fda||dfdf");
    vector<string> tokens;
    boost::regex reg("\\|\\|");
    boost::sregex_token_iterator it(str.begin(), str.end(), reg, -1);
    boost::sregex_token_iterator end;
    while (it!=end)
    {
        tokens.push_back(*it++);
    }
    for (size_t i = 0; i < tokens.size(); ++ i)
    {
        cout << tokens[i] << endl;
    }
    return 0;
}

 

C++常用字符串分割方法

标签:

原文地址:http://www.cnblogs.com/bohaoist/p/5041236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!