C++ 如何获取 std::string 的尾部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7597260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the tail of a std::string?
提问by Ronald McBean
How to retrieve the tail of a std::string
?
如何检索 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?
可选问题:如何使用Boost字符串算法库来实现?
ADDED:
添加:
The method should be save even if the original string is smaller than 6 chars.
即使原始字符串小于 6 个字符,该方法也应保存。
回答by Matthieu M.
There is one caveat to be aware of: if substris called with a position past the end of the array (superior to the size), then an out_of_range
exception is thrown.
有一点需要注意:如果调用substr的位置超过数组末尾(高于大小),则会out_of_range
引发异常。
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);
回答by CharlesB
Using the substr()
method and the size()
of the string, simply get the last part of it:
使用substr()
方法和size()
字符串的 ,只需获取它的最后一部分:
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!)
对于处理小于尾部大小的字符串的情况,请参阅Benoit 的答案(并赞成它,我不明白为什么我得到 7 个赞成票,而 Benoit 提供了更完整的答案!)
回答by Benoit
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.
请注意,这npos
是默认参数,可能会被省略。如果字符串的大小超过 6,则此例程将提取整个字符串。
回答by raines
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:
甚至更短,因为 subst 将字符串结尾作为第二个参数的默认值:
string str("This is a test");
string sub = str.substr(std::max<int>(str.size()-6,0));
回答by nabulke
Since you also asked for a solution using the boostlibrary:
由于您还要求使用boost库的解决方案:
#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());
}
回答by nabulke
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
回答by Jerome B.
Try the following:
请尝试以下操作:
std::string tail(&source[(source.length() > 6) ? (source.length() - 6) : 0]);
回答by galinette
string tail = source.substr(source.size() - min(6, source.size()));
回答by galinette
I think, using iterators is C++ way
我认为,使用迭代器是 C++ 的方式
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;
}