开发者

How to get the tail of a std::string?

How to retrieve the tail of a std::string?

If wishes could come true, it would work like that:

string tailString = sourceString.right(6);

But this seems to be too easy, and doesn't work...

Any nice solution available?

Optional question: How to do it with the Boost string algorithm library?

ADDED:

The method should be save eve开发者_开发问答n if the original string is smaller than 6 chars.


There is one caveat to be aware of: if substr is called with a position past the end of the array (superior to the size), then an out_of_range exception is thrown.

Therefore:

std::string tail(std::string const& source, size_t const length) {
  if (length >= source.size()) { return source; }
  return source.substr(source.size() - length);
} // tail

You can use it as:

std::string t = tail(source, 6);


Using the substr() method and the size() of the string, simply get the last part of it:

string tail = source.substr(source.size() - 6);

For handling case of a string smaller than the tail size see Benoit's answer (and upvote it, I don't see why I get 7 upvotes while Benoit provides a more complete answer!)


You could do:

std::string tailString = sourceString.substr((sourceString.length() >= 6 ? sourceString.length()-6 : 0), std::string::npos);

Note that npos is the default argument, and might be omitted. If your string has a size that 6 exceeds, then this routine will extract the whole string.


This should do it:

string str("This is a test");
string sub = str.substr(std::max<int>(str.size()-6,0), str.size());

or even shorter, since subst has string end as default for second parameter:

string str("This is a test");
string sub = str.substr(std::max<int>(str.size()-6,0));


You can use iterators to do this:

   #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;
    }

Output:

cut  is: esting


Since you also asked for a solution using the boost library:

#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());
} 


Try substr method.


I think, using iterators is C++ way

Something like that:

#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;
}


Try the following:

std::string tail(&source[(source.length() > 6) ? (source.length() - 6) : 0]);


string tail = source.substr(source.size() - min(6, source.size()));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜