string_alg简明用法(2)

首次用markdown写篇文章

本篇主要有如下字符串的操作:

  • 修剪
  • 查找
  • 替换/删除
  • 分割
  • 合并
  • 查找(分割)迭代器

每个函数基本有四个版本:
原版表示大小写敏感,在原串上操作。
前缀有i的表示大小写不敏感。
后缀有copy的表示不在原串上操作,返回操作后的结果。
后缀有if的表示可以增加判断条件谓词。

1.修剪

主要就是 trim 搭配 copyif 后缀,可选left,right

代码如下:

void Trim_Use()
{
    using namespace boost;
    format fmt("\|%s\|\n");
    std::string str(" some text ");
    std::cout << fmt % trim_copy(str);
    std::cout << fmt % trim_right_copy(str);

    trim_left(str);
    std::cout << fmt % str;

    std::string str2("123 some text !!");
    std::cout << fmt % trim_left_copy_if(str2, is_digit());
    std::cout << fmt % trim_right_copy_if(str2,is_punct());
    std::cout << fmt % trim_copy_if(str2,is_punct() ||is_space() || is_digit());
}

输入结果:

|some text|
| some text|
|some text |
| some text !!|
|123 some text |
|some text|

2.查找

主要是find搭配 firstnthlastheadtail
查找的结果存一个iterator_range的迭代器中,包含了字符串信息和位置信息。
示例代码如下:

void Find_Use()
{
    using namespace boost;
    format fmt("\|%s\|. pos = %d \n");
    std::string str("Long long ago, there was a king.");

    iterator_range<std::string::iterator> rge;
    rge = find_first(str, "long");
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge = ifind_first(str, "long");
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_nth(str, "ng",2);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_head(str,4);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_tail(str,5);
    std::cout << fmt % rge % (rge.begin() - str.begin());

    rge =find_first(str,"notexist");
    assert(rge.empty() && !rge);
    //TODO
    //find
    //find_token
    //find_regex
}

运行结果:

|long|. pos = 5 
|Long|. pos = 0 
|ng|. pos = 29 
|Long|. pos = 0 
|king.|. pos = 27 

3.替换/删除

主要使用replace/erase搭配firstnthlastallheadtail
同样都有copy后缀版本。
前8个有i前缀版本。
一个有20个。(2*(4*2+2))
示例代码:

void Replace_Delete_Use()
{
    using namespace boost;
    std::string str("There are some text and text.\n");

    std::cout << replace_first_copy(str, "This", "this");

    replace_last(str, "text", "string");
    std::cout << str;

    replace_tail(str,8, "text.\n");
    std::cout << str;

    std::cout << ierase_all_copy(str, "t");
    std::cout << replace_nth_copy(str,"t",3,"T");
    std::cout << erase_tail_copy(str, 8) << std::endl;
}

输出结果:

There are some text and text.
There are some text and string.
There are some text and text.
here are some ex and ex.
There are some text and texT.
There are some text an

4.分割

示例代码:

void Split_Use()
{
    using namespace boost;
    std::string str("Red,Green.Yellow::Black-White+Yellow");
    std::deque<std::string> d;
    ifind_all(d, str, "yELLOW");
    assert(d.size() == 2);
    for (BOOST_AUTO(pos, d.begin()); pos != d.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    std::list<iterator_range<std::string::iterator> > ls;
    split(ls, str, is_any_of(",.:-+"));

    for (BOOST_AUTO(pos, ls.begin()); pos != ls.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    ls.clear();
    split(ls, str, is_any_of(".:-"),token_compress_on);

    for (BOOST_AUTO(pos, ls.begin()); pos != ls.end(); ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;
}

输出结果:

[Yellow] [Yellow] 
[Red] [Green] [Yellow] [] [Black] [White] [Yellow] 
[Red,Green] [Yellow] [Black] [White+Yellow] 

5.合并

示例代码:

void Merger_Use()
{
    using namespace boost::assign;
    std::vector<std::string> v = list_of("Red")("Yellow")("Black")("Green");
    std::cout << boost::join(v, "+") << std::endl;;

    struct is_contains_e
    {
        bool operator()(const std::string & x)
        {
            return boost::contains(x, "e");
        }
    };

    std::cout << boost::join_if(v, "**", is_contains_e()) << std::endl;;
}

输出结果:

Red+Yellow+Black+Green
Red**Yellow**Green

6.查找(分割)迭代器

示例代码:

void Find_Spilt_Iterator()
{
    using namespace boost;
    std::string str("Red||red||yellow||black");
    typedef find_iterator<std::string::iterator> string_find_iterator;
    string_find_iterator pos, end;
    pos = make_find_iterator(str,first_finder("red", is_iequal()));
    for (; pos !=end; ++pos)
    {
        std::cout << "[" << *pos << "] ";
    }
    std::cout << std::endl;

    typedef split_iterator<std::string::iterator> string_split_iterator;

    string_split_iterator p, endp;
    p = make_split_iterator(str, first_finder("||", is_iequal()));
    for (; p != endp; ++p)
    {
        std::cout << "[" << *p << "] ";
    }
    std::cout << std::endl;

}

输出结果:

[Red] [red] 
[Red] [red] [yellow] [black] 

总结

时间仓促,只来得急帖代码。有空再来补充说明。
关于头文件。
format需要#include <boost/format.hpp>
BOOST_AUTO需要#include <boost/typeof/typeof.hpp>
list_of需要#include <boost/assign.hpp>
其余的都是STL里需要包含的。

这一系类函数可以完成绝大多数对字符串的处理了。

你可能感兴趣的:(字符串处理)