previous | start | next

## Strings (Substrings)

- The `substr(start, length)` member function is used to extract substrings.

  >     string greeting = "Hello, World!\n";
  >     string sub = greeting.substr(0,4);
  >     /* sub is "Hell" */

- The position numbers in a string start at 0. The last character is always one less than the length of the string.

  > `H` | `e` | `l` | `l` | `o` | `,` |  | `W` | `o` | `r` | `l` | `d` | `!` | `\n`
  > 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13

- `string w = greeting.substr(7, 5);`

  > `H` | `e` | `l` | `l` | `o` | `,` |  | `W` | `o` | `r` | `l` | `d` | `!` | `\n`
  > 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13

previous | start | next