- string[meta header]
- std[meta namespace]
- basic_string[meta class]
- function[meta id-type]
basic_string substr(size_type pos = 0, size_type n = npos) const;部分文字列を取得する。
pos番目からn要素の文字列を返す。
pos <= size()
nとsize() - posのうち、小さい方をコピーする長さrlenとして、
basic_string(data()+pos, rlen)
を返す。パラメータnのデフォルト引数であるnposの場合には、pos番目以降の全体を返す。
pos > size()の場合、out_of_range例外を送出する。
#include <iostream>
#include <string>
int main()
{
const std::string s = "hello";
// 2番目から3要素だけ抜き出した部分文字列を取得する
{
std::string result = s.substr(2, 3);
std::cout << result << std::endl;
}
// 2番目以降の全体からなる部分文字列を取得する
{
std::string result = s.substr(2);
std::cout << result << std::endl;
}
}- substr[color ff0000]
llo
llo