David.Sparky
David.Sparky

Reputation: 435

C++ equivalent of Python String Slice?

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:

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

Would print: Pear Orange

Upvotes: 37

Views: 58568

Answers (7)

whoan
whoan

Reputation: 8521

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 <iostream>
#include <string>

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

See it run


If you can use C++17, use a string_view to avoid a copy:

std::string_view(text).substr(6)

If you can use C++20, now we have ranges. See other answers for more information.

Upvotes: 47

Samar
Samar

Reputation: 19

**First parameter determines the starting index and the second parameter specifies the ending index remember that the starting of a string is from 0 **

string s="Apple";

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

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

Upvotes: 1

Refael Ackermann
Refael Ackermann

Reputation: 1558

It looks like C++20 will have Ranges https://en.cppreference.com/w/cpp/ranges which are designed to provide, amongst other things, python-like slicing http://ericniebler.com/2014/12/07/a-slice-of-python-in-c/ So I'm waiting for it to land in my favorite compiler, and meanwhile use https://ericniebler.github.io/range-v3/

Upvotes: 9

Tarc
Tarc

Reputation: 370

In C++ the closest equivalent would probably be string::substr(). Example:

std::string str = "Something";
printf("%s", str.substr(4)); // -> "thing"
printf("%s", str.substr(4,3)); // -> "thi"

(first parameter is the initial position, the second is the length sliced). Second parameter defaults to end of string (string::npos).

Upvotes: 17

cziemba
cziemba

Reputation: 664

Sounds like you want string::substr:

std::string text = "Apple Pear Orange";
std::cout << text.substr(6, std::string::npos) << std::endl; // "Pear Orange"

Here string::npos is synonymous with "until the end of the string" (and is also default but I included it for clarity).

Upvotes: 5

Mark B
Mark B

Reputation: 96241

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.

Note that unlike python, the first doesn't do range checking: Your input string needs to be long enough. Depending on your end-use for the slice there may be other alternatives as well (such as using an iterator range directly instead of making a copy like I do here).

Upvotes: 7

Lawrence Aiello
Lawrence Aiello

Reputation: 4638

You can do something like this using the string class:

std::string text = "Apple Pear Orange";
size_t pos = text.find('Pear');

Upvotes: 2

Related Questions