用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
| 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() |
| 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() |
| 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() |
| 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() |
| 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() |
Boost.String_Algo 的函数功能是相当的丰富了,也简便易用。所以可以在开发过程中大量使用。
BTW:有很多的人都排斥Boost,在一开始的时候我也不使用Boost,因为有太多的人建议我不要用。后来我还是使用了Boost,而且越用越喜欢,也越发感觉到Boost是一个多么灵活,高效,强大的C++ Library;也体会到了为什么可以称Boost具有”工业强度”。在这里,我建议那些不愿意使用Boost的朋友去使用一下Boost,也许你们是听别人建议不要用Boost,也许是自己不愿意使用Boost,但你们一定要去试用Boost中的一些可能用得上的Library,也许用了之后你们的看法会有所改变,也许你们会有不同的发现
评论
发表评论 反向链接