Syntax:
#include <string> string substr( size_type index = 0, size_type length = npos ) const;
substr()関数は現在の文字列の”index”から始まり”length”長の部分文字列を返します。
もし、”index + length”が文字列よりも長かった場合は、substr()関数の返り値は”index”から最後まで全部になります。
“length”が省略された場合にはデフォルトとしてstring::nposが使用され、substr()関数の返り値は”index”から最後まで全部になります。
サンプル:
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;
出力結果:
The original string is What we have here is a failure to communicate
The substring is a failure to communicate
Related Topics: copy