C++ equivalent of Python String Slice?

前端 未结 6 1253
灰色年华
灰色年华 2021-01-01 13:30

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 Code

6条回答
  •  情书的邮戳
    2021-01-01 14:19

    Yes, it is the substr method:

    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()).

    Example

    #include 
    #include 
    
    int main(void) {
        std::string text("Apple Pear Orange");
        std::cout << text.substr(6) << std::endl;
        return 0;
    }
    

    See it run

提交回复
热议问题