C++ Boost String Algorithms Library

03 Jun, 2019
The Boost String Algorithms Library provides a generic implementation of string-related algorithms which are missing in STL. It is an extension to the algorithms library of STL and it includes trimming, case conversion, predicates and find/replace functions. All of them come in different variants so it is easier to choose the best fit for a particular need.
The implementation is not restricted to work with a particular container (like basic_string), rather it is as generic as possible. This generalization is not compromising the performance since algorithms are using container specific features when it means a performance gain.
Converting a string to uppercase and lowercase.STL has a nice way of converting the character case. Unfortunately, it works only for a single character and we want to convert a string,to_upper() and to_lower() convert the case of characters in a string using a specified locale.to_upper_copy() and to_lower_copy() returns the copy of the converted string.Examples:#include <boost/algorithm/string.hpp>#include <iostream>  using namespace std;using namespace boost::algorithm;  int main(){    string str("Hello");    string upper_s;    string lower_s;      cout << "Actual string: "         << str << endl;      to_upper(str);    cout << "Actual string converted to uppercase: "         << str << endl;      to_lower(str);    cout << "Actual string converted to lowercase: "         << str << endl;      str = "Hello";    upper_s = to_upper_copy(str);    lower_s = to_lower_copy(str);      cout << "Converted Uppercase string: "         << upper_s << endl;    cout << "Converted Lowercase string: "         << lower_s << endl;      return 0;}Output:Actual string: Hello
Actual string converted to uppercase: HELLO
Actual string converted to lowercase: hello
Converted Uppercase string: HELLO
Converted Lowercase string: hello

STL has a nice way of converting the character case. Unfortunately, it works only for a single character and we want to convert a string,
to_upper() and to_lower() convert the case of characters in a string using a specified locale.
to_upper_copy() and to_lower_copy() returns the copy of the converted string.
Examples:
#include <boost/algorithm/string.hpp>#include <iostream>  using namespace std;using namespace boost::algorithm;  int main(){    string str("Hello");    string upper_s;    string lower_s;      cout << "Actual string: "         << str << endl;      to_upper(str);    cout << "Actual string converted to uppercase: "         << str << endl;      to_lower(str);    cout << "Actual string converted to lowercase: "         << str << endl;      str = "Hello";    upper_s = to_upper_copy(str);    lower_s = to_lower_copy(str);      cout << "Converted Uppercase string: "         << upper_s << endl;    cout << "Converted Lowercase string: "         << lower_s << endl;      return 0;}
Actual string: Hello
Actual string converted to uppercase: HELLO
Actual string converted to lowercase: hello
Converted Uppercase string: HELLO
Converted Lowercase string: hello

To remove characters from a stringBoost.StringAlgorithms provides several functions you can use to delete individual characters from a string.erase_first_copy() will remove the first occurrence in the source string.erase_nth_copy() will remove the nth occurrence in the source string.erase_all_copy() will remove all occurrences of a particular character from a string.To shorten a string by a specific number of characters on either end, use the functions erase_head_copy() and erase_tail_copy().Example#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>  using namespace std;using namespace boost::algorithm;  int main(){      string s = "geeksforgeeks";    cout << erase_first_copy(s, "g") << '\n';    cout << erase_nth_copy(s, "g", 0) << '\n';    cout << erase_last_copy(s, "g") << '\n';    cout << erase_all_copy(s, "g") << '\n';    cout << erase_head_copy(s, 5) << '\n';    cout << erase_tail_copy(s, 1) << '\n';    return 0;}Output:eeksforgeeks
eeksforgeeks
geeksforeeks
eeksforeeks
forgeeks
geeksforgeek

Boost.StringAlgorithms provides several functions you can use to delete individual characters from a string.
erase_first_copy() will remove the first occurrence in the source string.
erase_nth_copy() will remove the nth occurrence in the source string.
erase_all_copy() will remove all occurrences of a particular character from a string.
To shorten a string by a specific number of characters on either end, use the functions erase_head_copy() and erase_tail_copy().
Example
#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>  using namespace std;using namespace boost::algorithm;  int main(){      string s = "geeksforgeeks";    cout << erase_first_copy(s, "g") << '\n';    cout << erase_nth_copy(s, "g", 0) << '\n';    cout << erase_last_copy(s, "g") << '\n';    cout << erase_all_copy(s, "g") << '\n';    cout << erase_head_copy(s, 5) << '\n';    cout << erase_tail_copy(s, 1) << '\n';    return 0;}
eeksforgeeks
eeksforgeeks
geeksforeeks
eeksforeeks
forgeeks
geeksforgeek

To replace characters from a stringBoost.StringAlgorithms provides several functions you can use to replace individual characters from a string.replace_first_copy() will replace the first occurrence in the source string.replace_nth_copy() will replace the nth occurrence in the source string.replace_all_copy() will replace all occurrences of a particular character from a string.Example#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>  using namespace std;using namespace boost::algorithm;  int main(){      string s = "geeks_for_geeks";    cout        << replace_first_copy(s, "_", "-")        << '\n';    cout        << replace_last_copy(s, "_", "-")        << '\n';    cout        << replace_all_copy(s, "_", "-")        << '\n';    return 0;}Output:geeks-for_geeks
geeks_for-geeks
geeks-for-geeks

Boost.StringAlgorithms provides several functions you can use to replace individual characters from a string.
replace_first_copy() will replace the first occurrence in the source string.
replace_nth_copy() will replace the nth occurrence in the source string.
replace_all_copy() will replace all occurrences of a particular character from a string.
Example
#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>  using namespace std;using namespace boost::algorithm;  int main(){      string s = "geeks_for_geeks";    cout        << replace_first_copy(s, "_", "-")        << '\n';    cout        << replace_last_copy(s, "_", "-")        << '\n';    cout        << replace_all_copy(s, "_", "-")        << '\n';    return 0;}
geeks-for_geeks
geeks_for-geeks
geeks-for-geeks

Reference: https://www.boost.org/doc/libs/1_70_0/doc/html/string_algo/reference.html
cpp-boost
cpp-string
C++ Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
C Program to Swap two Numbers
Enumerated Types or Enums in C++
Passing a function as a parameter in C++
How to find the minimum and maximum element of a Vector using STL in C++?
Const keyword in C++
cin in C++
cout in C++
Program to implement Singly Linked List in C++ using class