1. Overview
C ++ 98 provides the string standard class STD: string. it has some member functions that can search for substrings, access characters, and perform basic string processing functions. since STD: String conforms to the container definition, it can also be considered as a sequence container with the element type char (or wchar T), and can be computed using a standard algorithm, however, the standard algorithm is not customized for string processing, and is often "clumsy ".
The appearance of the string_algo library has changed this embarrassing situation. It is a very comprehensive string algorithm library
A large number of string operation functions, such as case-insensitive comparison, trimming, and searching for substrings in a specific mode.
The string_algo library is located in the namespace boost: algorithm. To use the string_algo component, the header file <boost/algorithm/string. HPP> must be included. String_algo is also a group of classes, which is equivalent to a sub library. Its location is in the boost/algorithm/string directory. The main content is as follows:
Case_conv.hpp: case-sensitive conversion, including to_lower_copy, to_lower, to_upper_copy, to_upper
Classification. HPP: character category, including is_classified (STD: ctype_base: mask, locale), is_space, is_alnum, is_upper, is_xdigit, and other methods
Cllection_traits.hpp: a large number of traits classes used to unify various collection classes of char, such as STL collections, C-style array, and null-terminated C-strings.
Compare. HPP: String character functor, which provides two predicates: is_equal and is_iequal.
Concept. HPP: concept definition, including finderconcept and formatterconcept
Constants. HPP: defines an Enum token_compress_mode_type
Erase. HPP: provides a set of methods to remove characters and substrings from a string.
Find. HPP: provides a set of methods to find substrings from a string, allowing you to specify various substrings to search.
Finder. HPP: defines a set of methods for generating string finder.
Formatter. HPP: defines a set of methods for generating string formatter.
Predicate. HPP: provides a set of predictate, including starts_with, ends_withs, and contains.
Replace. HPP: provides a set of methods to replace substrings from strings.
Split: HPP: provides a group of methods to split substrings.
Trim. HPP: trim algorithm.
2. Naming rules
The algorithm naming in the string_algo library follows the convention of the standard library. The algorithm names are in lower case and different prefixes or suffixes are used to distinguish different versions:
Prefix I:This prefix indicates that the algorithm is case insensitive; otherwise, it is case sensitive:
Suffix copy:This suffix indicates that the algorithm returns a copy of the processing result without changing the input.
Suffix if:This suffix indicates that the algorithm needs a predicate function object of the implicit type. Otherwise, the default judgment criterion is used.
3. algorithm Classification
The string_algo Library provides five algorithms:
1. case-insensitive Conversion
2. Classification and Classification
3. Trim
4. search and replace
5. Segmentation and well Integration
4. Example: 3.1-3.3
Void test_string_case () {// returns an uppercase copy. The original string changes STD: String str1 ("I don't know. "); Boost: to_upper (str1); STD: cout <" str1 = "<str1 <STD: Endl; // returns an uppercase copy, the original string does not change STD: String str2 ("I don't know. "); STD: String str3 = boost: to_upper_copy (str2); STD: cout <" str2 = "<str2 <STD: Endl; STD :: cout <"str3 =" <str3 <STD: Endl;} void test_string_trim () {STD: String str1 = "ABC"; STD: String str2 = B Oost: trim_left_copy (str1); STD: String str3 = boost: trim_right_copy (str1); STD: String str4 = boost: trim_copy (str1 ); assert (str2 = "ABC"); Assert (str3 = "ABC"); Assert (str4 = "ABC"); STD: String str5 = "0005918580058 "; STD: String str6 = boost: trim_left_copy_if (str5, boost: is_any_of ("0"); STD: cout <str6 <STD: Endl ;} void test_string_precidate () {// starts_withassert (boost: starts_with ("boost_pytho N-vc100-mt-1_49.dll "," Boost "); Assert (! Boost: starts_with ("boost_python-vc100-mt-1_49.dll", "Boost"); Assert (boost: istarts_with ("boost_python-vc71-mt-1_33.dll", "Boost"); // ends_withassert (boost :: ends_with ("boost_python-vc100-mt-1_49.dll ",". DLL "); Assert (! Boost: ends_with ("boost_python-vc100-mt-1_49.dll ",". DLL "); Assert (boost: iends_with (" boost_python-vc100-mt-1_49.dll ",". DLL "); // contains assert (boost: Contains (" boost_python-vc100-mt-1_49.dll "," Python "); Assert (! Boost: Contains ("boost_python-vc100-mt-1_49.dll", "Python"); Assert (boost: icontains ("boost_python-vc100-mt-1_49.dll", "Python"); // export sassert (boost :: equals ("Boost", "Boost"); Assert (! Boost: equals ("Boost", "Boost"); Assert (boost: iequals ("Boost", "Boost"); // empty string testassert (boost:: starts_with ("boost_python-vc100-mt-1_49.dll", ""); Assert (boost: ends_with ("boost_python-vc100-mt-1_49.dll", ""); Assert (boost: Contains ("boost_python-vc100-mt-1_49.dll ", ""); // lexicalgrephical_compareassert (boost: lexicographical_compare ("boost_python-vc100-mt-1_49.dll", "boost_system-vc10 0-mt-1_49.dll "); // All: This condition is true if all its elements meet a given condition described by the limit. Assert (boost: All ("\ x20 \ t \ n \ r", boost: is_space (); Assert (boost :: all ("\ x20 \ t \ n \ r", boost: is_classified (STD: ctype_base: space); Assert (boost :: all ("\ x20 \ t \ n \ r", boost: is_any_of ("\ x20 \ t \ n \ r"); Assert (boost :: all ("ABCDE", boost: is_from_range ('A', 'E'); Assert (boost: All ("ABCDE", boost :: is_from_range ('A', 'z'); Assert (! Boost: All ("ABCDE", boost: is_from_range ('B', 'C'); Assert (boost: All ("ABC _ de ", boost: is_from_range ('A', 'z') | boost: is_space () | boost: is_any_of ("_");} void test_string_classify () {// is_space: whether the character is a space // is_alnum: whether the character is a letter or digit // is_alpha: whether the character is a letter // is_cntrl: whether the character is a control character // is_digit: whether the character is a decimal number // is_graph: whether the character is a graphical character // is_lower: whether the character is a lowercase character // is_print: whether the character is printable character // is_punct: whether the character is a punctuation character // is_upper: whether the character is an uppercase character // is_xdigit: whether the character is a hexadecimal number // is_any_of: whether the character is any character in the parameter Character Sequence // if_from_range whether the character is within the specified range, that is, from <= CH <=}