Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string tailString = sourceString.right(6);
- std::string tail(std::string const& source, size_t const length) {
- if (length >= source.size()) { return source; }
- return source.substr(source.size() - length);
- } // tail
- std::string t = tail(source, 6);
- string tail = source.substr(source.size() - 6);
- string str("This is a test");
- string sub = str.substr(std::max<int>(str.size()-6,0), str.size());
- string str("This is a test");
- string sub = str.substr(std::max<int>(str.size()-6,0));
- std::string tailString = sourceString.substr((sourceString.length() >= 6 ? sourceString.length()-6 : 0), std::string::npos);
- #include <iostream>
- #include <string>
- using namespace std;
- int main ()
- {
- char *line = "short line for testing";
- // 1 - start iterator
- // 2 - end iterator
- string temp(line);
- if (temp.length() >= 8) { // probably want at least one or two chars
- // otherwise exception is thrown
- int cut_len = temp.length()-6;
- string cut (temp.begin()+cut_len,temp.end());
- cout << "cut is: " << cut << endl;
- } else {
- cout << "Nothing to cut!" << endl;
- }
- return 0;
- }
- cut is: esting
- #include "boost/algorithm/string/find.hpp"
- std::string tail(std::string const& source, size_t const length)
- {
- boost::iterator_range<std::string::const_iterator> tailIt = boost::algorithm::find_tail(source, length);
- return std::string(tailIt.begin(), tailIt.end());
- }
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <iterator>
- using namespace std;
- std::string tail(const std::string& str, size_t length){
- string s_tail;
- if(length < str.size()){
- std::reverse_copy(str.rbegin(), str.rbegin() + length, std::back_inserter(s_tail));
- }
- return s_tail;
- }
- int main(int argc, char* argv[]) {
- std::string s("mystring");
- std::string s_tail = tail(s, 6);
- cout << s_tail << endl;
- s_tail = tail(s, 10);
- cout << s_tail << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement