#include#include using namespace std; int main() { string str = "Hello World"; string leftSubstring = str.substr(0, 5); // extract first 5 characters from string cout << "Left Substring: " << leftSubstring << endl; return 0; }
Left Substring: Hello
#include#include using namespace std; int main() { string str = "ABCDEFGH"; string leftString = str.left(5); // extract first 5 characters from string cout << "Left Substring: " << leftString << endl; return 0; }
Left Substring: ABCDEIn this example, we declare a string 'str' and initialize it with "ABCDEFGH". We use the left function to extract the first 5 characters of the string. This means, we get the substring "ABCDE" and store it in 'leftSubstring' variable. Finally, we print the leftSubstring using cout. Note: The left function returns a string, which we can store in a new variable. Package Library: The String left function is a part of the Standard Template Library (STL) in C++. Therefore, it is not part of any external package library.