C++常用字符串分割方法实例汇总

来源:互联网 发布:python代码大全 编辑:程序博客网 时间:2024/06/14 06:20
这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数、STL、Boost等常用的各类字符串分割方法,非常具有实用价值,需要的朋友可以参考下
<iframe width="580" height="90" align="center,center" id="cproIframe_u1892994_2" src="http://pos.baidu.com/acom?adn=3&amp;at=231&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=gb2312&amp;cfv=18&amp;ch=0&amp;col=zh-CN&amp;conBW=0&amp;conOP=1&amp;cpa=1&amp;dai=2&amp;dis=0&amp;ltr=http%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DcY-_f7dHD7fQgi6DVat0XBQPH8HD-4R6MbCMcL3HMcFq2JeAXDDf9uoHyuTRC5kB%26wd%3D%26eqid%3D8fa033b20000c7c40000000555cc39f6&amp;ltu=http%3A%2F%2Fwww.jb51.net%2Farticle%2F55954.htm&amp;lu_161=0&amp;lunum=6&amp;n=jb51_cpr&amp;pcs=1908x963&amp;pis=10000x10000&amp;ps=516x489&amp;psr=1920x1080&amp;pss=1908x517&amp;qn=674a5e1a20a6636d&amp;rad=&amp;rsi0=580&amp;rsi1=90&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23F7FCFF&amp;rss2=%230000ff&amp;rss3=%23444444&amp;rss4=%23008000&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=tabcloud_skin_3&amp;stid=5&amp;td_id=1892994&amp;titFF=%E5%AE%8B%E4%BD%93&amp;titFS=12&amp;titTA=left&amp;tn=text_default_580_90&amp;tpr=1439447566956&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u1892994&amp;ti=C%2B%2B%E5%B8%B8%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%86%E5%89%B2%E6%96%B9%E6%B3%95%E5%AE%9E%E4%BE%8B%E6%B1%87%E6%80%BB_C%20%E8%AF%AD%E8%A8%80_%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6&amp;tt=1439447566946.50.75.75" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true"></iframe>

本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下:

我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。

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

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

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

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

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

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

示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//借助strtok实现split
#include <string.h>
#include <stdio.h>
 
int main()
{
    chars[] = "Golden Global   View,disk * desk";
    constchar *d = " ,*";
    char*p;
    p =strtok(s,d);
    while(p)
    {
        printf("%s\n",p);
        p=strtok(NULL,d);
    }
 
    return0;
}

运行效果如下图所示:

二、用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)
返回值:子字符串

实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern)
{
  std::string::size_type pos;
  std::vector<std::string> result;
  str+=pattern;//扩展字符串以方便操作
  intsize=str.size();
 
  for(inti=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;
    }
  }
  returnresult;
}

完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
    File   : split1.cpp
    Author  : Mike
    E-Mail  : Mike_Zhang@live.com
 */
#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;//扩展字符串以方便操作
  intsize=str.size();
 
  for(inti=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;
    }
  }
  returnresult;
}
 
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(inti=0; i<result.size(); i++)
  {
    std::cout<<result[i]<<std::endl;
  }
 
  std::cin.get();
  std::cin.get();
  return0;
}

运行效果如下图所示:

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
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++);
    }
    returnvec;
}

完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//本程序实现的是利用正则表达式对字符串实现分割
//运行环境   VC6.0 + boost 库
/*
    File   : split2.cpp
    Author  : Mike
    E-Mail  : Mike_Zhang@live.com
*/
#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++);
    }
    returnvec;
}
int main()
{
    std::string str,s;
    str="sss/ddd/ggg/hh";
    s="/";
    std::vector<std::string> vec=split(str,s);
    for(inti=0,size=vec.size();i<size;i++)
    {
        std::cout<<vec[i]<<std::endl;
    }
    std::cin.get();
    std::cin.get();
    return0;
}

运行效果如下图所示:

补充:

最近发现boost里面有自带的split的函数,如果用boost的话,还是直接用split的好,这里就不多说了,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
  
usingnamespace 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;
 return0;
}

希望本文所述对大家的C++程序设计有所帮助。

0 0
原创粉丝点击