Published Jun 2, 2022Updated Dec 21, 2022
Contribute to Docs

The .substr() method returns a portion of a string specified by a starting position and length.


  • baseString is the string whose substring is being returned.
  • pos is the zero-based starting position of the substring. An exception is thrown if pos is greater than .size().
  • len is the number of characters to return in the substring. If len is greater than the number of characters after pos, the whole remainder of the string is returned.

Codebyte Example

The following example prints out a section of a longer string.


All contributors

Looking to contribute?

Learn C++ on Codecademy