Answers for "how to get a section of a string in c++"

C++
0

how to get a section of a string in c++

// string::substr
#include <iostream>
#include <string>
using namespace std;

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

  str2 = str.substr (12,12); // "generalities"

  pos = str.find("live");    // position of "live" in str
  str3 = str.substr (pos);   // get from "live" to the end

  cout << str2 << ' ' << str3 << endl;

  return 0;
}
Posted by: Guest on February-10-2021

Code answers related to "how to get a section of a string in c++"

Browse Popular Code Answers by Language