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裏需要包含的。

這一系類函數可以完成絕大多數對字符串的處理了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章