c\c++字符串(string)拆分并把字符串转成ASCII码

// sssssssss.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include<string>
#include<cstdio>
#include <vector>
using namespace std;
//对字符串进行拆分
vector<string> split(const string& str, const string& delim) {
	vector<string> res;
	char *next_token = NULL;
	if ("" == str) return res;
	//先将要切割的字符串从string类型转换为char*类型
	char * strs = new char[str.length() + 1]; //不要忘了
	strcpy_s(strs, str.length() + 1, str.c_str());
	char * d = new char[delim.length() + 1];
	strcpy_s(d, str.length() + 1, delim.c_str());
	
	char *p = strtok_s(strs, d,&next_token);
	while (p) {
		string s = p; //分割得到的字符串转换为string类型
		res.push_back(s); //存入结果数组
		p = strtok_s(NULL, d, &next_token);
	}
	return res;
}
//int类型的字符串转asci
int main()
{
	string st = "110,105,104,97,111,49,50,51";
	string sta = "";
	std::vector<string> res = split(st, ",");
	for (int i = 0; i < res.size(); ++i)
	{
		int aa = atoi(res[i].data());
		char c = toascii(aa);
		sta += c;
	}
	cout << sta << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34227896/article/details/84390821