// Rick Mercer 5-Oct-1995 // Demonstrate substrings and concatenation // #include // Borland's 4.5 cstring works (except for empty) #include #include void main() { string s1, s2 ; s1 = "abcDEFghiHKLabcDEF" ; // Beginning at s1[8], create and display a 5 character substring cout << "s1.substr(8, 5) == " << s1.substr(8, 5) << endl ; assert(s1.substr(8, 5) == "iHKLa") ; // Change s1 and s2 for use in the next several examples s1 = "A new" ; s2 = " string" ; // A string object, string literal, or char can be concatenated with += s1 += s2 ; // assert: s1's characters are "A new string" s1 += "--append me" ; // assert: s1's characters are "A new string--append me" s1 += '!' ; // assert: s1's characters are "A new string--append me!" cout << "\ns1: " << s1 << endl << endl ; // assert: 's1: A new string--append me!' is displayed // The following free functions are part of the string module: //-----relational operators // When comparing strings, remember that 'A' is considered less than 'B', // 'z' is greater than 'y'. Therefore "Able" is less than "Baker". // Think alphabeltically. s1 = "Able" ; s2 = "Baker" ; if(s1 < s2) cout << "True" ; if(s1 > s2) cout << "False" ; if(s1 <= s2) cout << "True" ; if(s1 >= s2) cout << "False" ; if(s1 == "Able") cout << "Yes" ; if(s1 != "Able") cout << "No" ; cout << endl ; // assert: 'TrueTrueYes' is output //-----The + concatenation operator s1 = "abc" ; s2 = "123" ; s1 = s1 + s2 ; // assert: s1 == "abc123" s1 = s1 + "xyz" ; // assert: s1 == "abc123xyz" s1 = "000" + s1 ; // assert: s1 == "000abc123xyz" s1 = '!' + s1 + '!' ; // assert: s1 == "!000abc123xyz!" cout << "\nAfter 4 concatenations, s1 == " << s1 << endl ; // assert: s1 == 'After 4 concatenations, s1 == !000abc123xyz!' }