读取由逗号分隔开的文本函数
2008-05-24 22:27
190 查看
读一块连续的文本字符串,由逗号或其它字符分隔成新的字符串,相当简单的函数及调用。
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
void split(const string& s,char ch,vector<string>& v)
{
//以指定字符分隔文本产生新的字符串
int i=0;
int j=s.find(ch);
while(j >= 0)
{
v.push_back(s.substr(i,j-i));
i = ++j;
j = s.find(ch,j);
if(j < 0)
v.push_back(s.substr(i,s.length()));
}
}
//调用
int main()
{
vector<string> vs;
string ss="abcd,student,market,hero";
split(ss,',',vs);//以逗号为分隔符
for(vector<string>::iterator p=vs.begin(); p != vs.end(); ++p)
{
cout<<"after split:"<<*p<<endl;
}
return 0;
}
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
void split(const string& s,char ch,vector<string>& v)
{
//以指定字符分隔文本产生新的字符串
int i=0;
int j=s.find(ch);
while(j >= 0)
{
v.push_back(s.substr(i,j-i));
i = ++j;
j = s.find(ch,j);
if(j < 0)
v.push_back(s.substr(i,s.length()));
}
}
//调用
int main()
{
vector<string> vs;
string ss="abcd,student,market,hero";
split(ss,',',vs);//以逗号为分隔符
for(vector<string>::iterator p=vs.begin(); p != vs.end(); ++p)
{
cout<<"after split:"<<*p<<endl;
}
return 0;
}
相关文章推荐
- java读取csv文件内容(逗号分隔、回车换行文本)
- gnuplot 读取逗号分隔的数据文件
- C语言从txt文本中读取多行用逗号分隔的数据
- C语言从文件中读取多行用逗号分隔数据的解决方案
- gnuplot 读取逗号分隔的数据文件
- Matlab 读取dat文件(包含几行多余文本,数据带有数字和字符串,且以逗号分隔)
- gnuplot 读取逗号分隔的数据文件
- 读取逗号分隔的字符串并逐条插入表mysql
- C语言从文件中读取多行用逗号分隔数据的解决方案
- CSV(逗号分隔)、文本文件(制表符分隔) 等文件的读取
- 读取用逗号分隔的文本文件
- C++ 读取和解析逗号分隔数据
- C++读取CSV文件,并将逗号分隔符文件分割成N个数组
- 有没有把数组直接转成以逗号分隔的字符串的函数
- java8新特性之逗号分隔字符串转List<Long>
- mysql:find_in_set 查找逗号分隔的数据
- 给定的逗号分隔的数字字符串转换为Table
- 从CSV文件中读取数据,使用逗号','分割问题
- Matlab读取txt中用空格分隔的数据文件到矩阵
- 如何相互转换逗号分隔的字符串和List