Python里面字符串的操作很方便,比如split,strip。在C++里,string提供了
length,得到字符串的長度,
append,在字符串末尾添加字符串,
push_back,在字符串末尾添加字符,
insert,指定位置處插入字符串,或n個字符,
assign,對字符串賦值,可以是某個字符串的從某個位置開始的多少個字符,也可以是常量字符串,也可以是指定個數的n個字符,
replace,用某個字符串,或者某個字符串的從某個位置開始的多少個字符,替換原字符串從某個位置開始的多少個字符,
erase,擦除指定區間的字符,
swap,兩個字符串的內容交換,
但是沒有提供split和strip,strip比較簡單,split很常用,但是還是需要好幾行代碼的。下面看看怎樣可以很好的實現split功能。
輸入一個字符串,分隔符,輸出是一個list,或者vector,
vector<string> split(string& s,const char *c);
很快便在cplusplus里面找到了一個示例,strtok,實現字符串的分割。
封裝成函數,如下,
vector<string> split(string& str,const char* c) { char *cstr, *p; vector<string> res; cstr = new char[str.size()+1]; strcpy(cstr,str.c_str()); p = strtok(cstr,c); while(p!=NULL) { res.push_back(p); p = strtok(NULL,c); } return res; }
由於使用了string,strtok,strcpy,vector,需要包含頭文件cstring,string,vector.
大概就7-8的代碼,因為使用了strtok,很簡單,或許C++不提供split,是因為已經有了strtok。
參考鏈接http://cplusplus.com/reference/string/string/c_str/。
網上有一篇討論split的,各種實現和效率的問題,可以看看。http://www.9php.com/FAQ/cxsjl/c/2008/09/3233721130092.html
還有cplusplus上專門討論split的,http://cplusplus.com/faq/sequences/strings/split/ 。
還有很多操作,如整型數據轉化為字符串,字符串轉化為整形數據,轉化為全大寫,全小寫等,匯總如下,
/* * stringenc.cpp * 2012-04-24 * some useful function for string manipulations, * including: * split, // split string by delim(such as " .,/") * int2str, // convert int to string * float2str, // convert double to string * str2int, // convert string to int * str2float, // convert string to double * strtoupper, // all to upper case * strtolower, // all to lower case * //strtocapital, // capital first character * */ #include <string> #include <cstring> #include <vector> #include <sstream> #include <algorithm> using namespace std; /** * @brief split a string by delim * * @param str string to be splited * @param c delimiter, const char*, just like " .,/", white space, dot, comma, splash * * @return a string vector saved all the splited world */ vector<string> split(string& str,const char* c) { char *cstr, *p; vector<string> res; cstr = new char[str.size()+1]; strcpy(cstr,str.c_str()); p = strtok(cstr,c); while(p!=NULL) { res.push_back(p); p = strtok(NULL,c); } return res; } /** * @brief convert a integer into string through stringstream * * @param n a integer * * @return the string form of n */ string int2str(int n) { stringstream ss; string s; ss << n; ss >> s; return s; } string float2str(double f) { stringstream ss; string s; ss << f; ss >> s; return s; } /** * @brief convert something to string form through stringstream * * @tparam Type Type can be int,float,double * @param a * * @return the string form of param a */ template<class Type> string tostring(Type a) { stringstream ss; string s; ss << a; ss >> s; } /** * @brief convert string to int by atoi * * @param s string * * @return the integer result */ int str2int(string& s) { return atoi(s.c_str()); } double str2float(string& s) { return atof(s.c_str()); } /** * @brief do string convert through stringstream from FromType to ToType * * @tparam ToType target type * @tparam FromType source type * @param t to be converted param * * @return the target form of param t */ template<class ToType,class FromType> ToType strconvert(FromType t) { stringstream ss; ToType a; ss << t; ss >> a; return a; } /** * @brief convert string to upper case throught transform method, also can use transform method directly * * @param s * * @return the upper case result saved still in s */ string& strtoupper(string& s) { transform(s.begin(),s.end(),s.begin(),::toupper); return s; } /** * @brief convert string to upper case through toupper, which transform a char into upper case * * @param s * * @return the upper case result string */ string strtoupper(string s) { string t = s; int i = -1; while(t[i++]) { t[i] = toupper(t[i]); } return t; } /** * @brief convert string to lower case throught transform method, also can use transform method directly * * @param s * * @return the lower case result saved still in s */ string& strtolower(string& s) { transform(s.begin(),s.end(),s.begin(),::tolower); return s; } /** * @brief convert string to lower case through tolower, which transform a char into lower case * * @param s * * @return the lower case result string */ string strtolower(string s) { string t = s; int i = -1; while(t[i++]) { t[i] = tolower(t[i]); } return t; }
如果有其他的,以后可以繼續添加。
在很多地方都有string操作詳解之類的,http://www.byvoid.com/blog/cpp-string/,而且文章風格很好,講解清晰詳盡,望塵莫及啊。
