一直被超越,从未被模仿

Craft Life, Don't let life Craft you.

Boost String Algorithms Library

来自Boost帮助文档

简介

Boost字符串算法库提供了很多操作字符串的算法:大小写转换、去掉字符串首尾空白字符或指定字符、查找、替换、分割等等。

使用方法

#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;

// ...

string str1(" hello world! ");
to_upper(str1);  // str1 == " HELLO WORLD! "
trim(str1);      // str1 == "HELLO WORLD!"

string str2=
	to_lower_copy(
		ireplace_first_copy(
			str1,"hello","goodbye")); // str2 == "goodbye world!"

命名规则

带_copy后缀的不修改源字符串,返回一个修改后的字符串副本,不带_copy后缀的则原地修改字符串。
带 i 前缀的表示该函数是大小写不敏感的。

字符串转换

string str1("HeLlO WoRld!");
to_upper(str1); // str1=="HELLO WORLD!"

谓词

// 是否是指定字符串结尾
bool is_executable( string& filename )
{
	return 
		iends_with(filename, ".exe") ||
		iends_with(filename, ".com");
}

// ...
string str1("command.com");
cout 
	<< str1
	<< (is_executable(str1)? "is": "is not") 
	<< "an executable" 
	<< endl; // prints "command.com is an executable"

//..
char text1[]="hello";
cout 
	<< text1 
	<< (all( text1, is_lower() )? " is": " is not")
	<< " written in the lower case" 
	<< endl; // prints "hello is written in the lower case"

Trimming

string str1="     hello world!     ";
string str2=trim_left_copy(str1);   // str2 == "hello world!     "
string str3=trim_right_copy(str1);  // str3 == "     hello world!"
trim(str1);                         // str1 == "hello world!"

string phone="00423333444";
// remove leading 0 from the phone number
trim_left_if(phone,is_any_of("0")); // phone == "423333444"

查找

char text[]="hello dolly!";
iterator_range<char*> result=find_last(text,"ll");

transform( result.begin(), result.end(), result.begin(), bind2nd(plus<char>(), 1) );
// text = "hello dommy!"            

to_upper(result); // text == "hello doMMy!"

// iterator_range is convertible to bool
if(find_first(text, "dolly"))
{
	cout << "Dolly is there" << endl;
}

替换

string str1="Hello  Dolly,   Hello World!"
replace_first(str1, "Dolly", "Jane");      // str1 == "Hello  Jane,   Hello World!"
replace_last(str1, "Hello", "Goodbye");    // str1 == "Hello  Jane,   Goodbye World!"
erase_all(str1, " ");                      // str1 == "HelloJane,GoodbyeWorld!"
erase_head(str1, 6);                       // str1 == "Jane,GoodbyeWorld!"

Find Iterator

string str1("abc-*-ABC-*-aBc");
// Find all 'abc' substrings (ignoring the case)
// Create a find_iterator
typedef find_iterator<string::iterator> string_find_iterator;
for(string_find_iterator It=
		make_find_iterator(str1, first_finder("abc", is_iequal()));
	It!=string_find_iterator();
	++It)
{
	cout << copy_range<std::string>(*It) << endl;
}

// Output will be:
// abc
// ABC
// aBC

typedef split_iterator<string::iterator> string_split_iterator;
for(string_split_iterator It=
	make_split_iterator(str1, first_finder("-*-", is_iequal()));
	It!=string_split_iterator();
	++It)
{
	cout << copy_range<std::string>(*It) << endl;
}

// Output will be:
// abc
// ABC
// aBC

分割字符串

string str1("hello abc-*-ABC-*-aBc goodbye");

typedef vector< iterator_range<string::iterator> > find_vector_type;

find_vector_type FindVec; // #1: Search for separators
ifind_all( FindVec, str1, "abc" ); // FindVec == { [abc],[ABC],[aBc] }

typedef vector< string > split_vector_type;

split_vector_type SplitVec; // #2: Search for tokens
split( SplitVec, str1, is_any_of("-*"), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye" }



阅读更多
文章标签: Boost
个人分类: Boost
上一篇Boost.Any
下一篇Docker 简介
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭