19.4. Substrings

Class string provides member function substr for retrieving a substring from a string. The result is a new string object that’s copied from the source string. Figure 19.3 demonstrates substr. The program declares and initializes a string at line 9. Line 13 uses member function substr to retrieve a substring from string1. The first argument specifies the beginning subscript of the desired substring; the second argument specifies the substring’s length.

 1   // Fig. 19.3: Fig19_03.cpp 2   // Demonstrating string member function substr. 3   #include <iostream> 4   #include <string> 5   using namespace std; 6  7   int main() 8   { 9      string string1( "The airplane landed on time." );10 11       ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.