How can I copy a substring from a given string with start and end index or giving the start index and length of the string are given.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From a
std::string,std::string::substrwill create a newstd::stringfrom an existing one given a start index and a length. It should be trivial to determine the necessary length given the end index. (If the end index is inclusive instead of exclusive, some extra care should be taken to ensure that it is a valid index into the string.)If you’re trying to create a substring from a C-style string (a NUL-terminated
chararray), then you can use thestd::string(const char* s, size_t n)constructor. For example:Unlike
std::string::substr, thestd::string(const char* s, size_t n)constructor can read past the end of the input string, so in this case you also should verify first that the end index is valid.