类似于C ++中java的string.split("")的函数 [英] Similar function to java's string.split(" ") in C++
本文介绍了类似于C ++中java的string.split("")的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找C ++中的类似函数 string.split(delimiter)
。它确实返回一个由指定分隔符剪切的字符串数组。 。
I am looking for the similar function in C++ to string.split(delimiter)
.It does return an array of strings cut by specified delimiter. .
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)
答案是好的答案,但作者删除了它。
THERE WAS A GOOD ANSWER BUT THE AUTHOR DELETED IT.
vector<string> split(string str, string sep){
char* cstr=const_cast<char*>(str.c_str());
char* current;
vector<std::string> arr;
current=strtok(cstr,sep.c_str());
while(current != NULL){
arr.push_back(current);
current=strtok(NULL, sep.c_str());
}
return arr;
}
推荐答案
您可以使用strtok。
http://www.cplusplus.com/reference/cstring/strtok/
You can use strtok. http://www.cplusplus.com/reference/cstring/strtok/
#include <string>
#include <vector>
#include <string.h>
#include <stdio.h>
std::vector<std::string> split(std::string str,std::string sep){
char* cstr=const_cast<char*>(str.c_str());
char* current;
std::vector<std::string> arr;
current=strtok(cstr,sep.c_str());
while(current!=NULL){
arr.push_back(current);
current=strtok(NULL,sep.c_str());
}
return arr;
}
int main(){
std::vector<std::string> arr;
arr=split("This--is--split","--");
for(size_t i=0;i<arr.size();i++)
printf("%s\n",arr[i].c_str());
return 0;
}
这篇关于类似于C ++中java的string.split("")的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文