replace_example.cpp 2.8 KB

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