Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. string tailString = sourceString.right(6);
  2.  
  3. std::string tail(std::string const& source, size_t const length) {
  4. if (length >= source.size()) { return source; }
  5. return source.substr(source.size() - length);
  6. } // tail
  7.  
  8. std::string t = tail(source, 6);
  9.  
  10. string tail = source.substr(source.size() - 6);
  11.  
  12. string str("This is a test");
  13. string sub = str.substr(std::max<int>(str.size()-6,0), str.size());
  14.  
  15. string str("This is a test");
  16. string sub = str.substr(std::max<int>(str.size()-6,0));
  17.  
  18. std::string tailString = sourceString.substr((sourceString.length() >= 6 ? sourceString.length()-6 : 0), std::string::npos);
  19.  
  20. #include <iostream>
  21. #include <string>
  22. using namespace std;
  23.  
  24. int main ()
  25. {
  26. char *line = "short line for testing";
  27. // 1 - start iterator
  28. // 2 - end iterator
  29. string temp(line);
  30.  
  31. if (temp.length() >= 8) { // probably want at least one or two chars
  32. // otherwise exception is thrown
  33. int cut_len = temp.length()-6;
  34. string cut (temp.begin()+cut_len,temp.end());
  35. cout << "cut is: " << cut << endl;
  36. } else {
  37. cout << "Nothing to cut!" << endl;
  38. }
  39. return 0;
  40. }
  41.  
  42. cut is: esting
  43.  
  44. #include "boost/algorithm/string/find.hpp"
  45.  
  46. std::string tail(std::string const& source, size_t const length)
  47. {
  48. boost::iterator_range<std::string::const_iterator> tailIt = boost::algorithm::find_tail(source, length);
  49. return std::string(tailIt.begin(), tailIt.end());
  50. }
  51.  
  52. #include <string>
  53. #include <iostream>
  54. #include <algorithm>
  55. #include <iterator>
  56. using namespace std;
  57.  
  58. std::string tail(const std::string& str, size_t length){
  59. string s_tail;
  60. if(length < str.size()){
  61. std::reverse_copy(str.rbegin(), str.rbegin() + length, std::back_inserter(s_tail));
  62. }
  63. return s_tail;
  64. }
  65.  
  66.  
  67. int main(int argc, char* argv[]) {
  68. std::string s("mystring");
  69. std::string s_tail = tail(s, 6);
  70. cout << s_tail << endl;
  71. s_tail = tail(s, 10);
  72. cout << s_tail << endl;
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement