#include <iostream> #include <string> using namespace std; void main() { int i = 0; string s1 = "ABCDEFG"; string s2 = s1; if (s1 == s2) cout << "s2 is a reference of s1" << endl; else cout << "s2 is a copy of s1" << endl; s1[0] = '0'; cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; if (s1 == s2) cout << "s2 is a reference of s1" << endl; else cout << "s2 is a copy of s1" << endl; cin >>i; }
#include <iostream> #include <string> using namespace std; void main() { int i = 0; char* p = "abcdefg"; string s1 = "ABCDEFGHIJKLMN"; string s2("OPQRSTUVWXYZ"); string s3(s1); string s4(s1, 0, 6); // string, offset, length string s5=s2; string s6(p); cout << "s1 = " << s1 << endl; cout << "s2 = " << s2 << endl; cout << "s3 = " << s3 << endl; cout << "s4 = " << s4 << endl; cout << "s5 = " << s5 << endl; cout << "s6 = " << s6 << endl; cin >> i; }
#include <iostream> #include <string> using namespace std; void main() { int i = 0; string s1 = "ABCDEFGHIJKLMN"; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; s1.insert(5, "abcd"); cout << "s1.insert(5, abcd)" << endl; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; s1.reserve(128); cout << "s1.reserve(128)" << endl; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; s1.append("xyz"); cout << "s1.append(xyz)" << endl; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; cin >> i; }
offset : 替换字符的起始位置。
#include <iostream> #include <string> using namespace std; int findandreplace(string oldstring, string targetstring, string newstring) { int i = 0; i = oldstring.find(targetstring); cout << "oldstring find targetstring = "<< targetstring << " pos = " << i << endl << endl; if (i == string::npos) { cout << "didn't find the target string" << endl; return -1; } cout << "replace the target string = " << "targetstring" << "in oldstring = " << oldstring << "wiht newstring = " << newstring << endl << endl; oldstring.replace(i, targetstring.size(), newstring); cout << "oldstring = " << oldstring << " oldstring.size() = " << oldstring.size() << " oldstring.capacity() = " << oldstring.capacity() << endl << endl; } void main() { int i = 0; string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE"; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; cout << "s1 replace BBBB with ######" << endl << endl; s1.replace(5, 4, "######"); cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; string s2 = "DDDD"; i = s1.find(s2); cout << "s1 find s2 = "<< s2 << " pos = " << i << endl << endl; cout << "replace the s2 in s1 wiht ^^^^^^" << endl << endl; s1.replace(i, s2.size(), "^^^^^^"); cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; cout << "use findandreplace function: can find the target string" << endl; findandreplace(s1, "EEEE", "&&&&&&"); cout << "use findandreplace function: can not find the target string" << endl; findandreplace(s1, "ABCD", "&&&&&&"); cin >> i; }
字符串的查找
#include <iostream> #include <string> using namespace std; void main() { int i = 0; string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD"; i = s1.find("BBBB"); cout << "find() BBBB in string "<< s1 << " the pos=" << i << endl << endl; s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD"; i = s1.find_first_of("BACD"); cout << "find_first_of() BACD in string "<< s1 << " the pos=" << i << endl << endl; i = s1.find_first_of("BBBB"); cout << "find_first_of() BBBB in string "<< s1 << " the pos=" << i << endl << endl; s1 = "AAAA-BCDE-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-AAAA"; i = s1.find_last_of("BBDD"); cout << "find_last_of() BBDD in string "<< s1 << " the pos=" << i << endl << endl; s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD"; i = s1.find_first_not_of("ABCD-"); cout << "find_first_not_of() ABCD- in string "<< s1 << " the pos=" << i << endl << endl; s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-DCBA-ABCD"; i = s1.find_last_not_of("ABCD-"); cout << "find_last_not_of() ABCD in string "<< s1 << " the pos=" << i << endl << endl; s1 ="ABCD-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-ABCD-DCBA-ABCD"; i = s1.rfind("ABCD"); cout << "rfind() ABCD in string "<< s1 << " the pos=" << i << endl << endl; cin >> i; }
offset: 表示删除的起始位置。
#include <iostream> #include <string> using namespace std; void main() { int i = 0; string s1 = "AAAA-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD-EEEE-BBBB-CCCC-DDDD"; cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; cout << "s1.erase(5, 5);" << endl << endl; s1.erase(5, 5); cout << "s1 = " << s1 << " s1.size() = " << s1.size() << " s1.capacity() = " << s1.capacity() << endl << endl; cin >> i; }
#include <iostream> #include <string> using namespace std; void main() { int i = 0; string s1 = "That"; string s2 = "This"; char* p1 = "Hello"; char* p2 = "That"; cout << "s1 == s2 : " << (s1 == s2) << endl; cout << "s1 != s2 : " << (s1 != s2) << endl; cout << "s1 > s2 : " << (s1 > s2) << endl; cout << "s1 >= s2 : " << (s1 >= s2) << endl; cout << "s1 < s2 : " << (s1 < s2) << endl; cout << "s1 <= s2 : " << (s1 <= s2) << endl; cout << "s1.compare(char* = Hello) : " << s1.compare(p1) << endl; cout << "s1.compare(char* = That ) : " << s1.compare(p2) << endl; cout << "s1.compare(s2) : " << s1.compare(s2) << endl; cout << "s1.compare(s1) : " << s1.compare(s1) << endl; cout <<" s1 = " << s1 << " s2 = " << s2 << endl; s1.swap(s2); cout << "s1.swap(s2)" << " s1 = " << s1 << " s2 = " << s2 << endl; cin >> i; }