python - C++ 相当于 Python 字符串切片?

标签 python c++ string

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

See it run

关于python - C++ 相当于 Python 字符串切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992264/

相关文章:

c++ - 高级 C++ 接口(interface) -- 跟踪 Kinect SKD 2.0 代码

java - 如何从字符串中进行插值搜索

c++ - sscanf c++ 将字符串拆分为整数有时不起作用

python - 在 Windows 中测试时,使用 fasttext api 的监督分类返回空数组

python - subprocess.Popen() - 顺序对于 p.stdout.read() 和 p.wait() 重要吗?

Python ETL - 使用 cx_Oracle 批量或迭代地将大型数据集加载到 Oracle 数据库中

python - 从文本文件读入 python 列表

c++ - 求解线性方程组的最有效方法

c++ - 奇怪的 C++ 链接错误

perl - 如何在Perl中删除哈希值的最后七个字符?