Boost String Algorithms Library 函数详解一 (case conversion和trimmming)

Boost String Algorithms Library 是很有用的一个库,它让C++程序员不用一次又一次的为了字符串的处理制造一些小小的轮子。这里我将逐一用实例详细的说明每一个函数的使用,希望给自己给同道带来方便。

Boost String Algorithms Library 是很有用的一个库,它让C++程序员不用一次又一次的为了字符串的处理制造一些小小的轮子。《浅尝boost之String algorithms library》里我主要介绍了它的Split ,因为这个功能很出彩,其它则一带而过。不过,当多次向朋友推荐这个库后,反馈得到的信息让我觉得详细列出其每一个函数的使用,作为一个程序员处理字符串时的手册,是件更有意义的事情。这里我将根据http://www.boost.org/doc/html/string_algo/quickref.html列表顺序,逐一用实例详细的说明每一个函数的使用,希望给自己给同道带来方便。

Table 11.1. Case Conversion

 

Algorithm name Description Functions
to_upper Convert a string to upper case to_upper_copy()
to_upper()
to_lower Convert a string to lower case to_lower_copy()
to_lower()



基础示例:

 1       //  _copy
 2         string  str1  =   " AABBccdd " ;
 3      assert(to_lower_copy(str1)  ==   " aabbccdd "   &&  str1 == " AABBccdd " );
 4      assert(to_upper_copy(str1)  ==   " AABBCCDD "   &&  str1 == " AABBccdd " );
 5       //
 6         string  str2  =  str1;
 7      to_lower(str2);
 8      assert(str2  ==   " aabbccdd " );
 9      str2  =  str1;
10      to_upper(str2);
11      assert(str2  ==   " AABBCCDD " );


注意:带_copy后缀的函数与不带_copy后缀的函数的区别,带_copy的函数不会修改源字符串,而是返回一个转换后的新字符串。

Table 11.2. Trimming

Algorithm name Description Functions
trim_left Remove leading spaces from a string trim_left_copy_if()
trim_left_if()
trim_left_copy()
trim_left()
trim_right Remove trailing spaces from a string trim_right_copy_if()
trim_right_if()
trim_right_copy()
trim_right()
trim Remove leading and trailing spaces from a string trim_copy_if()
trim_if()
trim_copy()
trim()


基础示例:

 1       //  _copy_if
 2         string  str1( "      space      " );
 3      assert(trim_left_copy_if(str1, is_space())  ==   " space      " );
 4      assert(trim_right_copy_if(str1, is_space())  ==   "      space " );
 5      assert(trim_copy_if(str1, is_space())  ==   " space " );
 6       //  _copy
 7      assert(trim_left_copy(str1)  ==   " space      " );
 8      assert(trim_right_copy(str1)  ==   "      space " );
 9      assert(trim_copy(str1)  ==   " space " );
10       //  _if
11       string  str2  =  str1;
12      trim_left_if(str2, is_space());
13      assert(str2  ==   " space      " );
14      str2  =  str1;
15      trim_right_if(str2, is_space());
16      assert(str2  ==   "      space " );
17      str2  =  str1;
18      trim_if(str2, is_space());
19      assert(str2  ==   " space " );
20       //
21      str2  =  str1;
22      trim_left(str2);
23      assert(str2  ==   " space      " );
24      str2  =  str1;
25      trim_right(str2);
26      assert(str2  ==   "      space " );
27      str2  =  str1;
28      trim(str2);
29      assert(str2  ==   " space " );


高级示例:

 

 

 1       // is_any_of()  非常重要的用法,可对付大部分的实际应用
 2       string  str3( " 123other " );
 3      assert(trim_left_copy_if(str3, is_any_of( " 321 " ))  ==   " other " );
 4       // std::back_inserter 
 5       string  str;
 6      trim_left_copy_if(std::back_inserter(str), str1, is_space());
 7      assert(str  ==   " space      " );
 8       // is_classified()
 9      assert(trim_copy_if(str3, is_classified(std::ctype_base::digit))   ==   " other " );
10       // 应用其它的 is_xxxxx方法
11      assert(trim_copy_if(str3, is_digit())   ==   " other " );
12       // 通过bind2nd,使用greater, less, less_equal等方法
13       string  str4( " acdxxxx " );
14      assert(trim_left_copy_if(str4, bind2nd(less < char > (), ' d ' ))  ==   " dxxxx " );

你可能感兴趣的:(algorithms)