在 python 中,我能够对字符串的一部分进行切片;换句话说,只是在某个位置之后打印字符。在 C++ 中是否有与此等价的功能?
Python 代码:
text= "Apple Pear Orange"
print text[6:]
将打印:梨橙
最佳答案
是的,它是 substr
方法:
basic_string substr( size_type pos = 0,
size_type count = npos ) const;
Returns a substring [pos, pos+count). If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos, size()).
示例
#include <iostream>
#include <string>
int main(void) {
std::string text("Apple Pear Orange");
std::cout << text.substr(6) << std::endl;
return 0;
}
关于python - C++ 相当于 Python 字符串切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992264/