C/C++字符串操作split

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/,而且文章风格很好,讲解清晰详尽,望尘莫及啊。

原文地址:https://www.cnblogs.com/Frandy/p/cpp_str_split.html