c++ 字符串分割函数stringsplit

新版: 

/*

 stringsplit.hpp

 sdragonx 2006-06-16 00:43:16
 revise 2018/7/28 19:42

 function list:

 size_t stringsplit(container, const char_type*, size_t, char_type, bool)
 size_t stringsplit(container, const char_type*, size_t, const char_type*, size_t, bool)

 这是个模板函数,支持标准库里面的各种容器,和标准字符串。
 这次更新,为了平台兼容性,削减到只剩两个核心函数。
 测试了3款编译器,编译通过 (gcc7.2.0、c++builder6.0、C++Builder2010)

 例如:
 std::string s = "abc,,123";
 std::vector<std::string> ls;
 stringsplit(ls, s.c_str(), s.length(), ',', true);
 如果repeat == true,s就被分割成为3个字符串,第二个字符串是空的
 如果repeat == false,s就被分割成2个字符串

 支持多个分割符:
 std::string s = "abc,|123|456";
 std::string spliter = ",|";
 std::vector<std::string> ls;
 stringsplit(ls, s.c_str(), s.length(), spliter.c_str(), spliter.size(), true);

 也支持其他容器,例如:
 std::list<std::string> ls;
 stringsplit(ls, s.c_str(), s.size(), ',', true);

 std::deque<std::string> ls;
 stringsplit(ls, s.c_str(), s.size(), ',', true);

 基础的两个函数,是以C字符串格式写的,也支持其他类型的字符串,
 只要这个字符串有构造函数String(const char*, size_t length)就行

 const char* s = "a,b,,c";
 std::vector<std::string> ls;
 stringsplit(ls, s, strlen(s), ',', true);

*/

#ifndef STRINGSPLIT_HPP_200606161656
#define STRINGSPLIT_HPP_200606161656

#include <algorithm>

namespace cgl{

//
// size_t stringsplit<char_type>(container, const char_type*, size_t, char_type, bool)
//
template<typename char_type, class container>
size_t stringsplit(
    container& ls,
    const char_type* str,
    size_t size,
    char_type spliter,
    bool repeat = true)
{
    typedef const char_type* const_iterator;
    typedef typename container::value_type string_type;

    const_iterator begin = str;
    const_iterator end = begin + size;
    const_iterator first = begin;
    const_iterator second;
    
    for( ; first<end; )
    {
        second = std::find<const_iterator>(first, end, spliter);
        if(first == second){
            if(repeat)ls.push_back(string_type());
        }
        else{
            ls.push_back(string_type(first, second - first));
    	}
        first = second+1;
    }
    if(repeat)
    {
        if(second == end-1){
        	ls.push_back(string_type());
        }
    }
    return ls.size();
}

//
// size_t stringsplit<char_type>(container, const char_type*, size_t, const char_type*, size_t, bool)
//
template<typename char_type, typename container>
size_t stringsplit(container& ls,
    const char_type* str,
    size_t size,
    const char_type* spliter,
    size_t spliter_size,
    bool repeat = true)
{
    typedef typename container::value_type string_type;
    typedef const char_type* const_iterator;
	
    const_iterator end = str + size;
    const_iterator first = str;
    const_iterator second;
    
    for( ; first<end; )
    {
        second = std::find_first_of<const_iterator>(first, end, spliter, spliter + spliter_size);
        if(first == second){
            if(repeat)ls.push_back(string_type());
        }
        else{
        	ls.push_back(string_type(first, second));
        }
        first = second+1;
    }
    if(repeat)
    {
        if(second == end-1){
        	ls.push_back(string_type());
        }
    }
    return ls.size();
}

/*

以下扩展函数作为参考
有的平台std::iterator是一个类,有点就是char*指针
各个平台实现方式不一样,暂时无法统一


//
// size_t stringsplit<string_type>(container, const_iterator, const_iterator, char_type, bool)
//
template<typename string_type, typename container>
size_t stringsplit(
    container& ls,
    typename string_type::const_iterator begin,
    typename string_type::const_iterator end,
    typename string_type::value_type spliter,
    bool repeat = true)
{
    return stringsplit(ls, &*begin, end - begin, spliter, repeat);
}

//
// size_t stringsplit<string_type>(container, string_type, char, bool)
//
template<typename string_type, typename container>
size_t stringsplit(
    container& ls,
    const string_type& str,
    typename string_type::value_type spliter,
    bool repeat = true)
{
    return stringsplit(ls, str.c_str(), str.size(), spliter, repeat);
}

//
// size_t stringsplit<string_type>(container, const_iterator, const_iterator, const_iterator, const_iterator, bool)
//
template<typename string_type, typename container>
size_t stringsplit(
    container& ls,
    typename string_type::const_iterator begin,
    typename string_type::const_iterator end,
    typename string_type::const_iterator spliter_begin,
    typename string_type::const_iterator spliter_end,
    bool repeat = true)
{
    return stringsplit(ls, &*begin, end - begin, &*spliter_begin, spliter_end - spliter_begin, repeat);
}

//
// size_t stringsplit<string_type>(container, string_type, string_type, bool)
//
template<typename string_type, typename container>
size_t stringsplit(
    container& ls,
    const string_type& str,
    const string_type& spliter,
    bool repeat = true)
{
    return stringsplit(ls, str.c_str(), str.size(), spliter.c_str(), spliter.size(), repeat);
}
*/

}; // end namespace cgl;

#endif //STRINGSPLIT_HPP_200606161656

以前的旧版: 

/*

 stringsplit.hpp

 sdragonx 2006-06-16 00:43:16
 revise  2016.07.18 19:04

*/

#ifndef STRINGSPLIT_HPP_200606161656
#define STRINGSPLIT_HPP_200606161656

#include <algorithm>

namespace cgl{

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(
	container< string_type<char_type> >& ls,
	typename string_type<char_type>::const_iterator begin,
	typename string_type<char_type>::const_iterator end,
	char_type spliter,
	bool repeat = true)
{
    if(end <= begin)
    {
    	return 0;
    }

    typename string_type<char_type>::const_iterator first = begin;
    typename string_type<char_type>::const_iterator second;
    
    for( ; first<end; )
    {
        second = std::find<string_type<char_type>::const_iterator>(first, end, spliter);
        if(first == second){
            if(repeat)ls.push_back(string_type<char_type>());
        }
        else{
            ls.push_back(string_type<char_type>(first, second));
    	}
        first = second+1;
    }
    if(repeat)
    {
        if(second == end-1){
        	ls.push_back(string_type<char_type>());
        }
    }
    return ls.size();
}

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(
	container< string_type<char_type> >& ls,
    typename string_type<char_type>::const_iterator begin,
    typename string_type<char_type>::const_iterator end,
    typename string_type<char_type>::const_iterator spliter_begin,
    typename string_type<char_type>::const_iterator spliter_end,
    bool repeat = true)
{
    if(end <= begin || spliter_end<=spliter_begin)
    {
    	return 0;
    }

    typename string_type<char_type>::const_iterator first = begin;
    typename string_type<char_type>::const_iterator second;
    
    for( ; first<end; )
    {
        second = std::find_first_of<string_type<char_type>::const_iterator>(first, end, spliter_begin, spliter_end);
        if(first == second){
            if(repeat)ls.push_back(string_type<char_type>());
        }
        else{
        	ls.push_back(string_type<char_type>(first, second));
        }
        first = second+1;
    }
    if(repeat)
    {
        if(second == end-1){
        	ls.push_back(string_type<char_type>());
        }
    }
    return ls.size();
}

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(container< string_type<char_type> > &strs,
	const string_type<char_type>& str, char_type spliter, bool repeat = true)
{
    return stringsplit(strs, str.begin(), str.end(), spliter, repeat);
}

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(container< string_type<char_type> > &strs,
	const char_type* str, size_t length, char_type spliter, bool repeat = true)
{
    return stringsplit(strs, str, str+length, spliter, repeat);
}

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(container< string_type<char_type> > &strs,
	const string_type<char_type>& str, const string_type<char_type>& spliter, bool repeat = true)
{
    return stringsplit(strs, str.begin(), str.end(), spliter.begin(), spliter.end(), repeat);
}

template<typename char_type, template<typename> class string_type, template<typename> class container>
size_t stringsplit(container< string_type<char_type> > &strs,
	const char_type* str, size_t length, const char_type* spliter, size_t splength, bool repeat = true)
{
    return stringsplit(strs, str, str+length, spliter, spliter+splength, repeat);
}

}; // end namespace cgl;

#endif //STRINGSPLIT_HPP_200606161656

 

C语言中的字符串分割函数可以有多种实现方式。其中一种常见的方法是使用循环遍历字符串,根据指定的分隔符将字符串分割成多个子串。通过引用的代码示例,我们可以看到一个简单的C语言分割字符串函数实现。该代码使用了字符串的find和substr方法来进行分割操作。它将字符串分割成多个子串,并将这些子串存储在一个vector容器中。然后通过遍历该容器,将每个子串打印出来。 另一种实现方法是使用正则表达式,通过引用的代码示例,我们可以看到使用regex_token_iterator进行字符串分割的方式。该函数使用了istringstream作为输入流,以指定的分隔符为分隔符,通过getline方法将字符串分割成多个子串,并将每个子串输出。 综上所述,C语言中的字符串分割函数可以根据具体需求选择不同的实现方式,可以使用循环遍历字符串并使用字符串的find和substr方法,也可以使用正则表达式进行分割。具体的实现方式取决于项目需求和个人偏好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++string如何实现字符串分割函数split()——4种方法](https://blog.csdn.net/m0_58086930/article/details/122759927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值