string s1 = "Object-Oriented "; string s2 = "Programming"; string s3 = s1.substr( 7, 9 ); string s4 = s1 + s2;
cout << s4 << endl;
s1 += s2;
cout << s1 << endl; s1.erase( 7, 9 );
cout << s1 << endl; s1.insert( 7, s3 );
cout << s1 << endl; s1.replace( 7, 9, "**" ); cout << s1 << endl;
查找和比较
int idx = s1.find( s2 ); if( idx < s1.length() )
cout << "Found at index: "<< idx << endl; else
cout << "Not found" << endl; if( s1 > s2 )
cout <<"\""+s1+"\""<<
" is greater than "<<"\""+s2+"\""<<endl; else
cout <<"\""+s1+"\""<<
" is not greater than "<<"\""+s2+"\""<<endl;