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:
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
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