[C++] 实现Split 字符串分割的几种方法

目录

单个字符作为分割

函数原型

方法一: 

 方法二:

 字符串作为分割

函数原型

方法一:

 方法二:

 代码测试

参考:


单个字符作为分割

函数原型

vector<string> Split(const string &s, const char &seperator);

方法一: 

istringstream类用于执行C++风格的串流的输入操作

istringstream的构造函数原形如下:

istringstream::istringstream(string str), 它的作用是从string对象str中读取字符

vector<string> Split(const string &s, const char &seperator) {
    vector<string> ans;
    istringstream stream(s);
    string word;
    while (getline(stream, word, seperator)) {
        ans.push_back(word);
    }
    return ans;
}

 方法二:

vector<string> Split(const string &s, const char &seperator) {
    vector<string> ans;
    string tmp = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != seperator) {
            tmp += s[i];
        } else {
            ans.push_back(tmp);
            tmp = "";
        }
    }
    ans.push_back(tmp);
    return ans;
}

 字符串作为分割

函数原型

vector<string> Split(const string &s, const string &seperator)

方法一:

vector<string> Split(const string &s, const string &seperator) {
	vector<string> ans;
	string token, str = s;
	size_t pos = 0;
	while((pos = str.find(seperator)) != string::npos) {
		token = str.substr(0, pos);
		ans.push_back(token);
		str.erase(0, pos + seperator.length());
	}
	ans.push_back(str);
	return ans;
}

 方法二:

vector<string> Split(const string &s, const string &seperator) {  
	vector<string> ans;  
	if(s == "") return ans;  
	
	//s -> char *
	char * strs = new char[s.length() + 1] ; //不要忘了  
	strcpy(strs, s.c_str());   
	
 	//seperator  -> char *
	char * d = new char[seperator.length() + 1];  
	strcpy(d, seperator.c_str());  
 
	char *p = strtok(strs, d);  
	while(p) {  
		string token = p; //分割得到的字符串转换为string类型  
		ans.push_back(token); //存入结果数组  
		p = strtok(NULL, d);  
	} 
	
 	delete[] strs;
 	delete[] d;
 	
	return ans;  
} 

 代码测试

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>

using namespace std;

vector<string> Split1(const string &s, const char &seperator) {
    vector<string> ans;
    istringstream stream(s);
    string word;
    while (getline(stream, word, seperator)) {
        ans.push_back(word);
    }
    return ans;
}

vector<string> Split2(const string &s, const char &seperator) {
    vector<string> ans;
    string tmp = "";
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != seperator) {
            tmp += s[i];
        } else {
            ans.push_back(tmp);
            tmp = "";
        }
    }
    ans.push_back(tmp);
    return ans;
}

vector<string> Split3(const string& s, const string &seperator) {
    vector<string> ans;
    if(s == "") return ans;

    //s -> char *
    char * strs = new char[s.length() + 1] ; //不要忘了  
    strcpy(strs, s.c_str());

    //seperator  -> char *
    char * d = new char[seperator.length() + 1];
    strcpy(d, seperator.c_str());

    char *p = strtok(strs, d);
    while(p) {
        string token = p; //分割得到的字符串转换为string类型  
        ans.push_back(token); //存入结果数组  
        p = strtok(NULL, d);
    }

    delete[] strs;
    delete[] d;

    return ans;
}

vector<string> Split4(const string &s, const string &seperator) {
    vector<string> ans;
    string token, str = s;
    size_t pos = 0;
    while((pos = str.find(seperator)) != string::npos) {
        token = str.substr(0, pos);
        ans.push_back(token);
        str.erase(0, pos + seperator.length());
    }
    ans.push_back(str);
    return ans;
}

int main() {
    string str1 = "hello c++ c java python php Go";
    string str2 = "192.168.10.254";
    string str3 = "88<=99<=100<=166";
    string str4 = "scott>=tiger>=mushroom";
    vector<string> v1 = Split1(str1, ' ');
    vector<string> v2 = Split2(str2, '.');
    vector<string> v3 = Split3(str3, "<=");
    vector<string> v4 = Split4(str4, ">=");
    for (auto &x: v1) cout << x << ' ';
    cout << endl;
    for (auto &x: v2) cout << x << ' ';
    cout << endl;
    for (auto &x: v3) cout << x << ' ';
    cout << endl;
    for (auto &x: v4) cout << x << ' ';
    return 0;
}

参考:https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
https://stackoverflow.blog/2019/10/11/c-creator-bjarne-stroustrup-answers-our-top-five-c-questions/?_ga=2.12029236.1470301215.1644821442-611235899.1620389838

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值