s.substr(pos,len);
// CPP program to illustrate substr()
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
// Take any string
string s1 = "Geeks";
// Copy three characters of s1 (starting
// from position 1)
string r = s1.substr(1, 3);
// prints the result
cout << "String is: " << r;
return 0;
}
// CPP program to illustrate substr()
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
// Take any string
string s1 = "Geeks";
// Copy two characters of s1 (starting
// from position 3)
string r = s1.substr(3, 2);
// prints the result
cout << "String is: " << r;
return 0;
}
#include <string>
#include <iostream>
int main() {
std::string innerPlanets{"Mercury Venus Earth Mars"};
// Copy 5 characters from position 8
std::cout << innerPlanets.substr(8, 5) << "
";
size_t begin = innerPlanets.find("Earth");
// Copy characters from "Earth" to the end of the string
std::cout << innerPlanets.substr(begin);
}
/ string::substr (pos, len)
#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); // "think" (pos, len)
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 << '
';
return 0;
}
/*
* this will be the output :--
* "think live in details."
*/
#include <iostream>
#include <string>
using namespace std;
void cpp_strings()
{
string unformatted_full_name { "StephenHawking" };
string first_name = unformatted_full_name.substr(0, 7);
// string first_name { unformatted_full_name, 0, 7 };
string last_name = unformatted_full_name.substr(7, 7);
string formatted_full_name = first_name + last_name;
formatted_full_name.insert(7, " ");
cout << formatted_full_name;
}
int main()
{
cpp_strings();
return 0;
}
string sub_string=main_string.substr(first_pos_of_the_substring_from_main_string,length_of_the_substring_from_main_string_starting_from_the_first_position
String is: ks
Code Example |
---|
Cpp :: tolower funciton in cpp |
Cpp :: how to make an overloaded constructor in c++ |
Cpp :: check if whole string is uppercase |
Cpp :: naive pattern matching algorithm |
Cpp :: console colors in C++ |
Cpp :: how to initialize array with new in c++ |
Cpp :: ue4 float to fstring |
Cpp :: c++ array rev pointer |
Cpp :: powershell get uptime remote computer |
Cpp :: loop through array c++ |
Cpp :: c++ encapsulation |
Cpp :: c++ string to int |
Cpp :: sorting vector elements c++ |
Cpp :: c++ vector resize |
Cpp :: cpp vs c# |
Cpp :: chudnovsky algorithm c++ |
Cpp :: cpp mutex |
Cpp :: sort array c++ |
Cpp :: how to use toString method in C++ |
Cpp :: new line in c++ |
Cpp :: iterate vector c++ |
Cpp :: c function as paramter |
Cpp :: how to say hello world in c++ |
Cpp :: c++ access second last element of vector |
Cpp :: strring length in c++ |
Cpp :: C++ Nested if...else |
Cpp :: string reverse iterator c++ |
Cpp :: oop in cpp |
Cpp :: how to change the value of a key in hashmp in c++ |
Cpp :: c++ open webpage |