Here are a few common examples:
#define I_end_with Boost::iends_with
#define I_start_with Boost::istarts_with
#define I_contain Boost::icontains
#define I_equal Boost::iequals
#define SPLIT Boost::algorithm::split
#define I_replace Boost::replace_all
To use Boost::algorithm you must first include the following header file
#include <boost/algorithm/string.hpp>
using namespace std;
First, letter capitalization conversion
1 To_upper () to capitalize the string
String str1 ("Hello world!");
To_upper (STR1); str1 = "HELLO world!"
2 to_upper_copy () converts a string to uppercase and assigns a value to another string
String str1 ("Hello world!");
String str2;
str2 = To_upper_copy (str1); str2 = "HELLO world!"
3 To_lower () to convert the string to lowercase
Example: Refer to To_upper ()
4 to_lower_copy () converts the string to lowercase and assigns a value to another string
Example: Refer to To_upper_copy ()
Two: Predicate
1 starts_with () to determine whether a string is a starting string for another
Example:
String str1 ("Hello world!");
String str2 ("Hello");
BOOL result = Starts_with (str1, str2); result = = True
2 istarts_with () determines whether a string is a start string of another string (case-insensitive)
Example:
String str1 ("Hello world!");
String str2 ("Hello");
BOOL result = Istarts_with (str1, str2); result = = True
3 ends_with () determines whether a string is the end string of another string
4 Iends_with () determines whether a string is the end string of another string (case-insensitive)
5 contains () to determine if a string contains another string
Example:
String str1 ("Hello world!");
String str2 ("Llo");
BOOL result = contains (str1, str2); result = = True
6 icontains () determines whether a string contains another string (case-insensitive)
7 equals () to determine whether two strings are equal
8 iequals () to determine whether two strings are equal (case-insensitive)
9 lexicographical_compare () sorted by dictionary, returns True if the first string is less than the second string (my boost1.33 is not implemented.) )
ilexicographical_compare () sorted by dictionary, returns True (case-insensitive) if the first string is less than the second string (my boost1.33 is not implemented.)
)
All () to determine whether all characters in the string satisfy this predicate
Example:
BOOL Is_123digit (const char &ch)
{
if (ch = = ' 1 ' | | | ch = = ' 2 ' | | ch = = ' 3 ')
return true;
Else
return false;
}
...
String str1 ("12332211");
BOOL result = ALL (str1, is_123digit); result = = True
str1 = "412332211";
result = All (str1, is_123digit); result = = False
Four: Find
1 find_first () looks up the substring in the string, returns the Iterator_range iterator of the substring in the original string
Example:
Char ToUpper (char &ch)
Char ToUpper (char &ch)
{
if (ch <= ' z ' && ch >= ' a ')
return ch + ' A '-' a ';
Else
return ch;
}
...
String str1 ("Hello dolly!");
iterator_range<string::iterator> result = Find_first (str1, "ll");
Transform (Result.begin (), Result.end (), Result.begin (), ToUpper); str1 = "HeLLo dolly!"
2 Ifind_first () to look up the substring in the string and return the Iterator_range iterator (case-insensitive) of this substring in the original string
3 find_last () finds the substring in the string from the tail, and returns the Iterator_range iterator of the substring in the original string
4 Ifind_last () finds the substring in the string from the tail, and returns the Iterator_range iterator (case-insensitive) of the substring in the original string.
5 Find_nth () finds the nth matching substring (calculated starting from 0)
Example:
String str1 ("Hello dolly!");
iterator_range<string::iterator> result = Find_nth (str1, "ll", 1);
Transform (Result.begin (), Result.end (), Result.begin (), ToUpper); str1 = "Hello dolly!"
6 Ifind_nth () finds nth matching substring (calculated starting from 0) (case-insensitive)
7 Find_head () to find the first n bytes of the string
Example:
String str1 ("Hello dolly!");
iterator_range<string::iterator> result = Find_head (str1,5);
Transform (Result.begin (), Result.end (), Result.begin (), ToUpper); str1 = "HELLO dolly!"
8 Find_tail () finds the latter n bytes of the string
9 Find_token () to find the string that matches the predicate
Example:
Char Add1 (const char &ch)
{
return ch+1;
}
...
String str1 ("Hello 1 world!");
iterator_range<string::iterator> result = Find_token (str1,is_123digit);
Transform (Result.begin (), Result.end (), Result.begin (), ADD1); str1 = = "Hello 2 world!");
Ten Find_regex () matching regular expressions
Example: (wait until you know the regular expression of boost and give it later)
One find () Use a lookup function written by yourself
example:
iterator_range<string:: Iterator>
myfinder1 (std::string::iterator begin, Std::string::iterator end)
{
std::string::iterator ITR;
for (ITR = begin;itr!=end;itr++)
{
if ((*itr) = = ' 1 ')
& nbsp; {
std::string::iterator preitr = ITR;
iterator_ range<string::iterator> ret (PREITR, ++ITR);
return ret;
}
}
return iterator_range<string::iterator> ();
}// boost also offers many finder
....
string str1 ("Hello 1 world!");
iterator_range<string::iterator> result = Find (Str1,myfinder1);
transform (Result.begin (), Result.end (), Result.begin (), Add1);//str1 = "Hello 2 world!");
V: delete/replace
1 replace_first () find the first matching string from the beginning and replace it with the given other string
Example:
String str1 ("Hello world!");
Replace_first (str1, "Hello", "Hello"); str1 = "Hello world!"
2 replace_first_copy () finds the first matching string from the beginning, replaces it with the given other string, and assigns
Value to another string
Example:
String str1 ("Hello world!");
String str2;
str2 = Replace_first_copy (str1, "Hello", "Hello"); str2 = "Hello world!"
3 Ireplace_first () finds the first matching string from scratch, replacing it with a given other string (case-insensitive
)
4 ireplace_first_copy () finds the first matching string from the beginning, replaces it with the given other string, and assigns
Value to another string (case-insensitive)
5 Erase_first () find the first matching string from scratch and delete it
Example:
String str1 ("Hello world!");
Erase_first (str1, "Llo"); str1 = "he world!"
6 erase_first_copy () finds the first matching string from scratch, deletes it, and assigns a value to another string
Example:
String str1 ("Hello world!");
String str2;
str2 = Erase_first_copy (str1, "Llo"); str2 = "he world!"
7 Ierase_first () find the first matching string from the beginning and delete it (case-insensitive)
8 Ierase_first_copy () finds the first matching string from scratch, deletes it, and assigns it to another string (without distinguishing the large
lowercase
Similar to the above, but starting from the end of the string to replace
9 Replace_last ()
Ten replace_last_copy ()
One ireplace_last ()
ireplace_last_copy ()
Erase_last ()
erase_last_copy ()
Ierase_last ()
ierase_last_copy ()
Similar to the above, but replaced from the start of the string n matches
Replace_nth ()
Example:
String str1 ("Hello world!");
Replace_nth (str1, "O", 1, "O"); str1 = "Hello world!"
replace_nth_copy ()
Ireplace_nth ()
ireplace_nth_copy ()
Erase_nth ()
erase_nth_copy ()
Ierase_nth ()
ierase_nth_copy ()
Similar to the above, but instead of replacing all the matching strings
Replace_all ()
Num replace_all_copy ()
Ireplace_all ()
ireplace_all_copy ()
Erase_all ()
erase_all_copy ()
Ierase_all ()
ierase_all_copy ()
replace_head () replace the first n characters
Example:
String str1 ("Hello world!");
Replace_head (STR1, 5, "HELLO"); str1 = "HELLO world!"
replace_head_copy () replaces the first n characters, and assigns a value to another string
Example:
String str1 ("Hello world!");
String str2;
str2 = Replace_head_copy (str1, 5, "HELLO"); str2 = "HELLO world!"
erase_head () Remove the first n characters
Example:
String str1 ("Hello world!");
Erase_head (STR1, 5); str1 = "world!"
erase_head_copy () deletes the first n characters and assigns a value to another string
Example:
String str1 ("Hello world!");
String str2;
str2 = Erase_head_copy (str1, 5); str2 = "world!"
Similar to above (n characters after replacement/deletion)
Panax Notoginseng Replace_tail ()
replace_tail_copy ()
Erase_tail ()
erase_tail_copy ()
Related to regular expressions, later understood.
Replace_regex ()
replace_regex_copy ()
Erase_regex ()
erase_regex_copy ()
Replace_all_regex ()
replace_all_regex_copy ()
Erase_all_regex ()
erase_all_regex_copy ()
Not very clear, I'll see you later.
Find_format ()
find_format_copy ()
Wuyi Find_format_all ()
find_format_all_copy ()
VI: Cutting
1 find_all () finds all matching values and places the values in the given container
Example:
String str1 ("Hello world!");
std::vector<std::string> result;
Find_all (result, str1, "L"); result = [3] ("L", "L", "L")
2 Ifind_all () finds all matching values and places the values in the given container (case-insensitive)
3 Find_all_regex () is associated with regular expressions and will later understand
4 split () cuts the string according to the given predicate and puts the cut value into the given container
Example:
Class Splitnotthischar
{
Public
Splitnotthischar (const char CH): M_char (ch) {}
BOOL Operator () (const char &ch) const
{
if (ch = = M_char)
return true;
Else
return false;
}
Private
Char M_char;
};
String str1 ("Hello world!");
String str2;
std::vector<std::string> result;
Split (result, str1, Splitnotthischar (' l ')); result = [4] ("he", "", "O wor", "d!")
5 Split_regex () is associated with regular expressions and will later understand
6 Iter_find () finds the string according to the given Finder and puts the found value in the given container
Example:
String str1 ("Hello world!");
std::vector<std::string> result;
First_finder for boost self-brought finder
Iter_find (result, str1, First_finder ("L")); result = [3] ("L", "L", "L")
7 iter_split () cut the string according to the given finder and put the cut value into the given container
Example:
String str1 ("Hello world!");
std::vector<std::string> result;
Iter_split (result, str1, First_finder ("L")); result = [4] ("he", "", "O wor", "d!")
Seven: Connection
My boost 1.33 is not implemented.
1 Join
2 join_if