相当于Python String Slice的C ++? [英] C++ equivalent of Python String Slice?

查看:98
本文介绍了相当于Python String Slice的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,我可以切片一部分字符串;换句话说,只需在特定位置之后打印字符。在C ++中有与此等效的东西吗?

In python I was able to slice part of a string; in other words just print the characters after a certain position. Is there an equivalent to this in C++?

Python代码:

text= "Apple Pear Orange"
print text[6:]

将打印: 梨橙

推荐答案

是,它是 substr 方法:

Yes, it is the substr method:

basic_string substr( size_type pos = 0,
                     size_type count = npos ) const;
    



返回子字符串[pos,pos + count)。如果请求的子字符串超出了字符串的末尾,或者计数== npos,则返回的子字符串为[pos,size())。

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 String Slice的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆