C++ Boost String Algorithms Library Last Updated : 03 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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: CPP #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 To remove characters from a string 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 CPP #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 To replace characters from a string 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 CPP #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 Reference: https://www.boost.org/doc/libs/1_70_0/doc/html/string_algo/reference.html Comment More infoAdvertise with us Next Article C++ Boost String Algorithms Library _udhay_ Follow Improve Article Tags : C++ cpp-string cpp-boost Practice Tags : CPP Similar Reads boost::trim in C++ library This function is included in the "boost/algorithm/string" library. The Boost String Algorithms Library provides a generic implementation of string-related algorithms which are missing in STL. The trim function is used to remove all leading or trailing white spaces from the string. The input sequence 2 min read Boyer-Moore Algorithm for Pattern Searching in C++ The Boyer-Moore algorithm is an efficient string searching algorithm that is used to find occurrences of a pattern within a text. This algorithm preprocesses the pattern and uses this information to skip sections of the text, making it much faster than simpler algorithms like the naive approach.In t 6 min read <strings> library in C++ STL Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return i 3 min read C++ String Class and its Applications C++ string class and its applications have more functions as discussed in this article String vs Character Array In C++, in addition to a character array, there exists a similar kind of way to implement string, that is using a string class which is a part of C++ standard library.We need to add a hea 4 min read C++ string class and its applications In C++ we can store string by one of the two ways â C style stringsstring class (discussed in this post) In this post, the second method is discussed. string class is part of C++ library that supports a lot much functionality over C style strings. C++ string class internally uses char array to store 6 min read Like