Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;相当于Python字符串切片?_Python_C++_String - Fatal编程技术网

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

C++;相当于Python字符串切片?,python,c++,string,Python,C++,String,在python中,我能够切掉字符串的一部分;换句话说,只需在某个位置后打印字符。在C++中是否有等价的? Python代码: text= "Apple Pear Orange" print text[6:] 将打印:梨橙听起来像你想要的: std::string text=“苹果梨橙”; std::cout您可以使用string类执行类似操作: std::string text = "Apple Pear Orange"; size_t pos = text.find('Pear'); st

在python中,我能够切掉字符串的一部分;换句话说,只需在某个位置后打印字符。在C++中是否有等价的?

Python代码:

text= "Apple Pear Orange"
print text[6:]
将打印:
梨橙

听起来像你想要的:

std::string text=“苹果梨橙”;

std::cout您可以使用string类执行类似操作:

std::string text = "Apple Pear Orange";
size_t pos = text.find('Pear');
std::string text=“苹果梨橙”;
std::cout是的,这是一种方法:

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

例子
#包括
#包括
内部主(空){
字符串文本(“苹果梨橙”);

STD::CUT

C++中最接近的等价物可能是String::SUBLUTE()。 例如:

(第一个参数是初始位置,第二个参数是切片长度)。
第二个参数默认为字符串的结尾(string::npos)。

看起来C++20将具有范围 设计用于提供类似python的切片 所以我在等待它进入我最喜欢的编译器,同时使用

**第一个参数确定起始索引,第二个参数指定结束索引请记住,字符串的起始是从0开始的**

string s="Apple";

string ans=s.substr(2);//ple

string ans1=s.substr(2,3)//pl
std::string text = "Apple Pear Orange";
std::cout << std::string(text.begin() + 6, text.end()) << std::endl;  // No range checking at all.
std::cout << text.substr(6) << std::endl; // Throws an exception if string isn't long enough.
basic_string substr( size_type pos = 0,
                     size_type count = npos ) const;
    
#include <iostream>
#include <string>

int main(void) {
    std::string text("Apple Pear Orange");
    std::cout << text.substr(6) << std::endl;
    return 0;
}
std::string str = "Something";
printf("%s", str.substr(4)); // -> "thing"
printf("%s", str.substr(4,3)); // -> "thi"
string s="Apple";

string ans=s.substr(2);//ple

string ans1=s.substr(2,3)//pl