본문 바로가기

알고리즘 문제풀이/알고리즘

[c++] substr 함수

http://www.cplusplus.com/reference/string/string/substr/

 

string::substr - C++ Reference

12345678910111213141516171819 // string::substr #include #include int main () { std::string str="We think in generalities, but we live in details."; // (quoting Alfred N. Whitehead) std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find

www.cplusplus.com

예제

// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // 3번째 char부터 5개의 char을 string으로 => "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end 끝까지!!

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}