C++ Boost String Algorithms Library
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>usingnamespacestd;usingnamespaceboost::algorithm;intmain(){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;return0;}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
#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>usingnamespacestd;usingnamespaceboost::algorithm;intmain(){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';return0;}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
#include <boost/algorithm/string.hpp>#include <cstdlib>#include <iostream>#include <string>usingnamespacestd;usingnamespaceboost::algorithm;intmain(){string s ="geeks_for_geeks";cout<< replace_first_copy(s,"_","-")<<'\n';cout<< replace_last_copy(s,"_","-")<<'\n';cout<< replace_all_copy(s,"_","-")<<'\n';return0;}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


