最終更新日時(UTC):
が更新

履歴 編集

function
<string_view>

std::basic_string_view::substr(C++17)

constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;

概要

部分文字列を取得する。

戻り値

指定された位置posからn文字からなる部分文字列を構築して返す。

size() - posnうち、小さい方をrlenとし、basic_string_view(data() + pos, rlen)を返す。

例外

pos > size()の場合、std::out_of_range例外を送出する。

#include <iostream>
#include <string_view>

int main()
{
  std::string_view sv = "This is a pen";

  std::string_view ret1 = sv.substr(5);    // 5番目から最後までの文字列を取得
  std::string_view ret2 = sv.substr(5, 2); // 5番目から2文字の文字列を取得

  std::cout << "1 : [" << ret1 << ']' << std::endl;
  std::cout << "2 : [" << ret2 << ']' << std::endl;

  // substrはデータを切り取るのではなく、参照位置と参照サイズを変更するだけなので、
  // 生ポインタを介せば全体の文字列を復元することはできる。
  const char* ret3 = ret1.data() - 5;
  std::cout << "3 : [" << ret3 << ']' << std::endl;
}

出力

1 : [is a pen]
2 : [is]
3 : [This is a pen]

バージョン

言語

  • C++17

処理系