首页 > 编程语言 >C++如何做字符串分割(5种方法)

C++如何做字符串分割(5种方法)

时间:2022-11-29 10:56:51浏览次数:61  
标签:std 分割 string pos C++ str 字符串 include size

C++如何做字符串分割(5种方法)

于 2021-04-06 17:54:50 发布55916 收藏 163 分类专栏: C++ 文章标签: C++ 字符串分割 版权 C++专栏收录该内容 44 篇文章6 订阅 订阅专栏

1、用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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

示例:

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3、find_first_not_of

#include<string>
#include<vector>
#include<iostream>
using namespace std;

void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
{
  // Skip delimiters at beginning.
  string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  // Find first "non-delimiter".
  string::size_type pos     = str.find_first_of(delimiters, lastPos);
  while (string::npos != pos || string::npos != lastPos)
  {
    // Found a token, add it to the vector.
    tokens.push_back(str.substr(lastPos, pos - lastPos));
    // Skip delimiters.  Note the "not_of"
    lastPos = str.find_first_not_of(delimiters, pos);
    // Find next "non-delimiter"
    pos = str.find_first_of(delimiters, lastPos);
  }
}

int main(int argc, char *argv[])
{
  string str("====aaa==bbb=ccc=ddd====");
  vector<string>tokens;
  Tokenize(str, tokens, "=");
  for( int i = 0; i < tokens.size() ; i++ )
  {
    cout << tokens[i] << endl;
  }
  return 0;
}
  • 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

4、用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;
}
  • 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

5、最近发现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;
}

标签:std,分割,string,pos,C++,str,字符串,include,size
From: https://www.cnblogs.com/zxdplay/p/16934759.html

相关文章

  • 【字符串】最小表示法
    目录​​一、概念​​​​二、模板​​​​三、例题​​​​题:899.有序队列​​​​解:​​一、概念最小表示法是用于解决字符串最小表示问题的方法。循环同构:当字符串S中......
  • 【小航的算法日记】字符串算法(二) - 字符串比较
    目录​​一、概念​​​​二、模板​​​​三、例题​​​​题:剑指Offer05.替换空格​​​​解:​​​​题:面试题10.05.稀疏数组搜索​​​​解:​​​​题:1763.最长的......
  • 【小航的算法日记】字符串
    目录​​一、概念​​​​二、模板​​​​三、例题​​​​题:344.反转字符串​​​​解:​​​​题:541.反转字符串II​​​​解:​​​​题:剑指Offer05.替换空格​​​......
  • C++多继承(多重继承)
    派生类都只有一个基类,称为单继承(SingleInheritance)。除此之外,C++也支持多继承(MultipleInheritance),即一个派生类可以有两个或多个基类。多继承容易让代码逻辑复杂、思路......
  • 在Unity中使用C#调用C++动态链接库(DLL)
     在Unity中使用C#调用C++动态链接库(DLL)https://blog.csdn.net/qq_51456342/article/details/125693678 [FNote: 属性页中无C++项时,要先写点代码编译一下,就有了]......
  • 1758. 生成交替二进制字符串的最少操作数
    1758.生成交替二进制字符串的最少操作数classSolution{publicintminOperations(Strings){char[]c=s.toCharArray();intn=c.length;......
  • c++_makefile
    关于四个version的思考,既然makefile的使用是为了便捷,从刚开始第一个简版的makefile有什么用,到第二版可以用依赖来抽象出1$(TARGET)和$(OBJ)之间的关系2.o.cpp之间的关......
  • Swift 2023:强调并发、泛型和 C++ 互操作性,开发 Swift 解析器
    AppleSwift团队的一名工程师兼语言工作组成员JohnMcCall在最新发布的一篇博客中介绍了Swift的2023年度计划。“Swift项目中有很多激动人心的工作正在进行,而且很......
  • C++学习------cmath头文件的源码学习06
    函数族定义---双曲函数cosh---计算双曲余弦函数sinh---计算双曲正弦函数tanh---计算双曲正切函数acosh---计算双曲余弦面积asinh---计算双曲正弦面积atanh---计算双曲正切面......
  • C++引用和指针
    1、有时候要想搞清楚一条赋值语句到底是改变了指针的值还是改变了指针所指对象的值不太容易,最好的办法就是记住赋值永远改变的等号左边的对象,例如:*p=0,改变的是p所指对象的......