The substr() member function of the basic_string C++ program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the substr(), basic_string class member function to copy a substring of at most some number of characters from a string beginning from a specified position in C++ programming
To show: How to use the substr() the basic_string class member function to copy a substring of at most some number of characters from a string beginning from a specified position in C++ programming
// the substr() C++ example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
string str1("Testing the substr()");
cout<<"str1 string is: "<<str1<<endl;
cout<<"\nOperation: str1.substr(4, 7)"<<endl;
basic_string <char> str2 = str1.substr(4, 7);
cout<<"The substring str1 copied is: "<<str2<<endl;
cout<<"\nOperation: str1.substr()"<<endl;
basic_string <char> str3 = str1.substr();
cout<<"The default str3 substring is: "<<str3
<<"\nwhich is the original string."<<endl;
return 0;
}
Output example:
str1 string is: Testing the substr()
Operation: str1.substr(4, 7)
The substring str1 copied is: ing the
Operation: str1.substr()
The default str3 substring is: Testing the substr()
which is the original string.
Press any key to continue . . .