Reputation: 4703
I want to slice array of character in c++ or simply equivivalent code of python code given below.
s1 = ["A", "B", "C" , "D" , "E"]
s2 = s1[0:2]
s2 ==> ["A","B"]
s1 ===> ["A", "B", "C" , "D" , "E"]
Upvotes: 1
Views: 1619
Reputation: 43108
std::string_View
is the answer if you are using a C++17 compiler. See Get part of a char array
auto s1 = "ABCDE";
auto s2 = std::string_view{s1, 2}; // AB
If you don't want to start at position 0, you can move the pointer further along and slice from there:
auto s2 = std::string_view{s1 + 2, 2}; // CD
Unfortunately, there is currently no option for changing the step value, as with python
Upvotes: 0
Reputation: 42083
You should use std::string
objects while working with strings and for retrieving some part of the string you can use std::string::substr
:
std::string s1("ABCDE");
std::string s2 = s1.substr(0, 2);
Upvotes: 4