用std::string来代替char*作为字符串处理的方式在现代的C++开发中已是约定俗成的规则了。不过std::string的公有成员函数的功能是很有限的,而且操作起来并不是很方便,借助于 std::algorithms 来操作std::string时,因为std::algorithms需要面对的是容器的通用操作,也以,也有部分我们所需要的std::string操作无法通过std::algorithms来实现。好在 String Algorithms Library 为std::string弥补了众多在操作上的不足之处。先看看 String Algorithms Library 的函数:

Algorithms

Table 21.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()

Table 21.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()

Table 21.3. Predicates

Algorithm name Description Functions
starts_with Check if a string is a prefix of the other one starts_with()
istarts_with()
ends_with Check if a string is a suffix of the other one ends_with()
iends_with()
contains Check if a string is contained of the other one contains()
icontains()
equals Check if two strings are equal equals()
iequals()
lexicographical_compare Check if a string is lexicographically less then another one lexicographical_compare()
ilexicographical_compare()
all Check if all elements of a string satisfy the given predicate all()

Table 21.4. Find algorithms

Algorithm name Description Functions
find_first Find the first occurrence of a string in the input find_first()
ifind_first()
find_last Find the last occurrence of a string in the input find_last()
ifind_last()
find_nth Find the nth (zero-indexed) occurrence of a string in the input find_nth()
ifind_nth()
find_head Retrieve the head of a string find_head()
find_tail Retrieve the tail of a string find_tail()
find_token Find first matching token in the string find_token()
find_regex Use the regular expression to search the string find_regex()
find Generic find algorithm find()

Table 21.5. Erase/Replace

Algorithm name Description Functions
replace/erase_first Replace/Erase the first occurrence of a string in the input replace_first()
replace_first_copy()
ireplace_first()
ireplace_first_copy()
erase_first()
erase_first_copy()
ierase_first()
ierase_first_copy()
replace/erase_last Replace/Erase the last occurrence of a string in the input replace_last()
replace_last_copy()
ireplace_last()
ireplace_last_copy()
erase_last()
erase_last_copy()
ierase_last()
ierase_last_copy()
replace/erase_nth Replace/Erase the nth (zero-indexed) occurrence of a string in the input replace_nth()
replace_nth_copy()
ireplace_nth()
ireplace_nth_copy()
erase_nth()
erase_nth_copy()
ierase_nth()
ierase_nth_copy()
replace/erase_all Replace/Erase the all occurrences of a string in the input replace_all()
replace_all_copy()
ireplace_all()
ireplace_all_copy()
erase_all()
erase_all_copy()
ierase_all()
ierase_all_copy()
replace/erase_head Replace/Erase the head of the input replace_head()
replace_head_copy()
erase_head()
erase_head_copy()
replace/erase_tail Replace/Erase the tail of the input replace_tail()
replace_tail_copy()
erase_tail()
erase_tail_copy()
replace/erase_regex Replace/Erase a substring matching the given regular expression replace_regex()
replace_regex_copy()
erase_regex()
erase_regex_copy()
replace/erase_regex_all Replace/Erase all substrings matching the given regular expression replace_all_regex()
replace_all_regex_copy()
erase_all_regex()
erase_all_regex_copy()
find_format Generic replace algorithm find_format()
find_format_copy()
find_format_all()
find_format_all_copy()()

Table 21.6. Split

Algorithm name Description Functions
find_all Find/Extract all matching substrings in the input find_all()
ifind_all()
find_all_regex()
split Split input into parts split()
split_regex()
iter_find Iteratively apply the finder to the input to find all matching substrings iter_find()
iter_split Use the finder to find matching substrings in the input and use them as separators to split the input into parts iter_split()

Table 21.7. Join

Algorithm name Description Functions
join Join all elements in a container into a single string join
join_if Join all elements in a container that satisfies the condition into a single string join_if()

Boost.String_Algo 的函数功能是相当的丰富了,也简便易用。所以可以在开发过程中大量使用。

BTW:有很多的人都排斥Boost,在一开始的时候我也不使用Boost,因为有太多的人建议我不要用。后来我还是使用了Boost,而且越用越喜欢,也越发感觉到Boost是一个多么灵活,高效,强大的C++ Library;也体会到了为什么可以称Boost具有”工业强度”。在这里,我建议那些不愿意使用Boost的朋友去使用一下Boost,也许你们是听别人建议不要用Boost,也许是自己不愿意使用Boost,但你们一定要去试用Boost中的一些可能用得上的Library,也许用了之后你们的看法会有所改变,也许你们会有不同的发现 :)

本站原创文章,转载请注明出处