substr [C++ Reference]
Keywords: string,substr
Date: 2010-02-26T06:27:07-0800

[[substr]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## substr

Syntax:

        #include <string>
        string substr( size_type index = 0, size_type length = npos ) const;

The `substr` method of the string class returns a substring of the current string, starting at `index`, and `length` characters long.

If `index + length` is past the end of the string, then only the remainder of the string starting at `index` will be returned.

If `length` is omitted, it will default to `string::npos`, and `substr` will simply return the remainder of the string starting at `index`.

For example:

         string s("What we have here is a failure to communicate");
         string sub = s.substr(21);
         cout << "The original string is " << s << endl;
         cout << "The substring is " << sub << endl;

displays

         The original string is What we have here is a failure to communicate
         The substring is a failure to communicate

Related Topics: copy

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •