| 1 | // Boost string_algo library example file ---------------------------------// |
| 2 | |
| 3 | // Copyright Pavol Droba 2002-2003. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // See http://www.boost.org for updates, documentation, and revision history. |
| 9 | |
| 10 | #include <string> |
| 11 | #include <iostream> |
| 12 | #include <iterator> |
| 13 | //#include <boost/algorithm/string/replace.hpp> |
| 14 | //#include <boost/algorithm/string/erase.hpp> |
| 15 | //#include <boost/algorithm/string/case_conv.hpp> |
| 16 | #include <boost/algorithm/string.hpp> |
| 17 | |
| 18 | //Following two includes contain second-layer function. |
| 19 | //They are already included by first-layer header |
| 20 | |
| 21 | //#include <boost/algorithm/string/replace2.hpp> |
| 22 | //#include <boost/algorithm/string/find2.hpp> |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace boost; |
| 26 | |
| 27 | // uppercase formatter |
| 28 | /* |
| 29 | Convert an input to upper case. |
| 30 | Note, that this formatter can be used only on std::string inputs. |
| 31 | */ |
| 32 | inline string upcase_formatter( |
| 33 | const iterator_range<string::const_iterator>& Replace ) |
| 34 | { |
| 35 | string Temp(Replace.begin(), Replace.end()); |
| 36 | to_upper(Input&: Temp); |
| 37 | return Temp; |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | cout << "* Replace Example *" << endl << endl; |
| 43 | |
| 44 | string str1("abc___cde___efg" ); |
| 45 | |
| 46 | // Erase 6-9th characters from the string |
| 47 | cout << "str1 without 6th to 9th character:" << |
| 48 | erase_range_copy( Input: str1, SearchRange: make_iterator_range(Begin: str1.begin()+6, End: str1.begin()+9) ) << endl; |
| 49 | |
| 50 | // Replace 6-9th character with '+++' |
| 51 | cout << "str1 with 6th to 9th character replaced with '+++': " << |
| 52 | replace_range_copy( |
| 53 | Input: str1, SearchRange: make_iterator_range(Begin: str1.begin()+6, End: str1.begin()+9), Format: "+++" ) << endl; |
| 54 | |
| 55 | cout << "str1 with 'cde' replaced with 'XYZ': " ; |
| 56 | |
| 57 | // Replace first 'cde' with 'XYZ'. Modify the input |
| 58 | replace_first_copy( Output: ostream_iterator<char>(cout), Input: str1, Search: "cde" , Format: "XYZ" ); |
| 59 | cout << endl; |
| 60 | |
| 61 | // Replace all '___' |
| 62 | cout << "str1 with all '___' replaced with '---': " << |
| 63 | replace_all_copy( Input: str1, Search: "___" , Format: "---" ) << endl; |
| 64 | |
| 65 | // Erase all '___' |
| 66 | cout << "str1 without all '___': " << |
| 67 | erase_all_copy( Input: str1, Search: "___" ) << endl; |
| 68 | |
| 69 | // replace third and 5th occurrence of _ in str1 |
| 70 | // note that nth argument is 0-based |
| 71 | replace_nth( Input&: str1, Search: "_" , Nth: 4, Format: "+" ); |
| 72 | replace_nth( Input&: str1, Search: "_" , Nth: 2, Format: "+" ); |
| 73 | |
| 74 | cout << "str1 with third and 5th occurrence of _ replace: " << str1 << endl; |
| 75 | |
| 76 | // Custom formatter examples |
| 77 | string str2("abC-xxxx-AbC-xxxx-abc" ); |
| 78 | |
| 79 | // Find string 'abc' ignoring the case and convert it to upper case |
| 80 | cout << "Upcase all 'abc'(s) in the str2: " << |
| 81 | find_format_all_copy( |
| 82 | Input: str2, |
| 83 | Finder: first_finder(Search: "abc" , Comp: is_iequal()), |
| 84 | Formatter: upcase_formatter ); |
| 85 | |
| 86 | cout << endl; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |