substring c++ code example
Example 1: std::substring
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
std::string str2 = str.substr (3,5);
std::size_t pos = str.find("live");
std::string str3 = str.substr (pos);
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Example 2: string substr c++
s.substr(pos,len);
Example 3: splice string in c++
string str1 = "Apples are red";
string str2 = str1.substr(11, 3);
string str3 = str1.substr(0, 6);
Example 4: how to get a section of a string in c++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="We think in generalities, but we live in details.";
string str2, str3;
size_t pos;
str2 = str.substr (12,12);
pos = str.find("live");
str3 = str.substr (pos);
cout << str2 << ' ' << str3 << endl;
return 0;
}