.substr()
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.
Syntax
baseString.substr(pos,len)
baseString
is the string whose substring is being returned.pos
is the zero-based starting position of the substring. An exception is thrown ifpos
is greater than.size()
.len
is the number of characters to return in the substring. Iflen
is greater than the number of characters afterpos
, the whole remainder of the string is returned.
Codebyte Example
The following example prints out a section of a longer string.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours