本文我们将实现两个python字符串分割函数。这两个函数的函数原型为:
//采用std的swap函数 void reverse_strings(std::vector< std::string > & result) { for (std::vector< std::string >::size_type i = 0; i < result.size() / 2; i++) { std::swap(result[i], result[result.size() - 1 - i]); } }
void split_whitespace(const std::string &str, std::vector<std::string> &result, int maxsplit) { std::string::size_type i, j, len = str.size(); for (i = j = 0; i < len;) { while (i < len&&::isspace(str[i])) i++; j = i; while (i < len&&!::isspace(str[i])) i++; if (j < i) { if (maxsplit-- <= 0) break; result.push_back(str.substr(j, i - j)); while (i < len&&::isspace(str[i])) i++; j = i; } } if (j < len) { result.push_back(str.substr(j, len - j)); } }
void split(const std::string &str, std::vector<std::string>&result, const std::string &sep, int maxslit) { result.clear(); if (maxslit < 0) maxslit = MAX_32BIT_INT; //MAX_32BIT_INT是自己定义的一个整数,当maxslit为负数时,对整个字符串做切割处理 //split函数默认为空格为分隔符 if (sep.size() == 0) { //调用函数进行空格切割 split_whitespace(str, result, maxslit); return; } std::string::size_type i, j, len = str.size(), n = sep.size(); i = j = 0; while (i + n <= len) { if (str[i] == sep[0] && str.substr(i, n)== sep) { if (maxslit-- <= 0) break; result.push_back(str.substr(j, i - j)); i = j = i + n; } else i++; } //剩下部分 result.push_back(str.substr(j, len - j)); }
void rsplit_whitespace(const std::string &str, std::vector<std::string>&result, int maxsplit) { std::string::size_type i,j,len = str.size(); for (i = j = len; i > 0;) { while (i > 0 && ::isspace(str[i - 1])) i--; j = i; while (i > 0 && !::isspace(str[i - 1])) i--; if (j > i) { if (maxsplit-- <= 0) break; result.push_back(str.substr(i, j - i)); while (i > 0 && ::isspace(str[i - 1])) i--; j = i; } } if (j > 0) { result.push_back(str.substr(0, j)); } reverse_strings(result); }
void rsplit(const std::string &str, std::vector<std::string>&result, const std::string &sep, int maxsplit) { if (maxsplit < 0) { split(str, result, sep, maxsplit); return; } result.clear(); if (sep.size() == 0) { rsplit_whitespace(str, result, maxsplit); return; } std::string::size_type i, j; std::string::size_type len = str.size(); std::string::size_type n = sep.size(); i = j = len; while (i >= n) { if (str[i - 1] == sep[n - 1] && str.substr(i - 1, n) == sep) { if (maxsplit-- <= 0) break; result.push_back(str.substr(i, n)); i = j = i - n; } else { i--; } } result.push_back(str.substr(0, j)); reverse_strings(result); }
string s = "I'm not to see you"; vector<string> result; string sep = " "; split(s,result,sep,10);
string b = "abc abc abc abc"; vector<string>result; string sep = "a"; split(b, result, sep, 2); for (int i = 0; i < result.size(); i++) cout << result[i] << endl;
string b = "abc abc abc abc"; vector<string>result; string sep = "a"; rsplit(b, result, sep, 2); for (int i = 0; i < result.size(); i++) cout << result[i] << endl;
#include <stdio.h> #include <string.h> int main() { char s[] ="ab|cdef;ghi|jkl"; char* delim = "|;"; char* tmp; tmp = strtok(s, delim); while(tmp != NULL) { printf("%s\n",tmp); tmp = strtok(NULL, delim); } return 0; }
#include <stdio.h> #include <string.h> /* @in, str: 待分割的字符串 @in, delim: 分隔符字符串 @in_out, dest: 保存分割后的每个字符串,设为char**的引用,即表示可修改其值 @out, pCount: 记录一行中分割所得字符串的个数 */ void split(char* str, char* delim, char** &dest, int* pCount) { char* tmp; *pCount=0; if(NULL == str || 0 == strlen(str)) return ; if(NULL == delim || 0 == strlen(delim)) return ; tmp = strtok(str, delim); while(tmp != NULL) { for(int j =0; tmp[j]!='\0';j++) { if(tmp[j]=='\n')break; //到达行末 (*dest)[j] = tmp[j]; } (*dest)[j]='\0'; dest++; (*pCount)++; tmp = strtok(NULL, delim); } } int main() { FILE* fp; char lineBuf[129]; char* delim = " "; //分隔符为:空格 int num = 0; //文件中总的字符串个数 int count = 0; //一行中的字符串个数 int i; /*申请内存用于存放字符串*/ char** dest = new char*[128]; for( i = 0; i < 128; i++) { dest[i] = new char[64]; } char** tmpDest = dest; if(fp=fopen("test.txt", "r")) { while(fgets(lineBuf, 128, fp) != NULL) { split(lineBuf, delim, tmpDest, &count); num = num + count; } } fclose(fp); for(i= 0; i < num; i++) { printf("%s\n",dest[i]); } /*释放内存*/ for(i = 0; i<128;i++) { delete []dest[i]; } delete[]dest; return 0; }
#include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; /* @in, src: 待分割的字符串 @in, delim: 分隔符字符串 @in_out, dest: 保存分割后的每个字符串 */ void split(const string& src, const string& delim, vector<string>& dest) { string str = src; string::size_type start = 0, index; string substr; index = str.find_first_of(delim, start); //在str中查找(起始:start) delim的任意字符的第一次出现的位置 while(index != string::npos) { substr = str.substr(start, index-start); dest.push_back(substr); start = str.find_first_not_of(delim, index); //在str中查找(起始:index) 第一个不属于delim的字符出现的位置 if(start == string::npos) return; index = str.find_first_of(delim, start); } } int main() { ifstream infile("test.txt", ios::in); vector<string> results; string word; string delim(" "); string textline; if(infile.good()) { while(!infile.fail()) { getline(infile, textline); split(textline, delim, results); } } infile.close(); vector<string>::iterator iter = results.begin(); while(iter != results.end()) { cout<<*iter++<<endl; } return 0; }
myfile = open('test.txt', 'r') allWords = [] line = myfile.readline() while line: list = line.split(' ') for word in list: if word[-1]=='\n': allWords.append(word[:-1]) #去掉行末的'\n' else: allWords.append(word) line = myfile.readline() myfile.close() print allWords References: |