1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include <string>
string s1; string s2("Hello"); string s3(4, 'k'); string s3; s3.assign(4, 'k'); string s4("12345", 1, 3); string s4; s3.assign("12345", 1, 3);
string s5,s6("hhhh"); s5 = s6; char s0 = 's'; s5 = s0;
s3 = s1 + s2;
string s7("123"),s8("abc"); s7.append(s8); s7.append(s8,1,2); s7.append(3,'k'); s7.append("abcde",2,3);
bool ret; ret = s2 > s3;
string s1("hello"), s2("hello, world"); int n=s1.compare(s2); n=s1.compare(1,2,s2,0,3); n=s1.compare(0,2,s2); n s1. compare("Hello"); n=s1.compare(1,2,"Hello"); n=s1.compare(1,2,"Hello",1,2);
string ss = s4.substr(1,3); ss = s4.substr(1);
find() rfind() find_first_of() find_last_of() find_first_not_of() find_last_not_of()
string s1("Real Steel"); s1.replace(1, 3, "123456", 2, 4); cout << s1 << endl;
string s2("Harry Potter"); s2.replace(2, 3, 5, '0'); cout << s2 << endl;
int n = s2.find("00000"); s2.replace(n, 5, "XXX"); cout << s2 << endl;
string s1("Real Steel"); s1.erase(1, 3); cout << s1 << endl; s1.erase(5); cout << s1 << endl;
string s1("Limitless"), s2("00");
s1.insert(2, "123"); cout << s1 << endl;
s1.insert(3, s2); cout << s1 << endl;
s1.insert(3, 5, 'X'); cout << s1 << endl;
int main() { char str[] = "Hello,world!This-is;a.test"; const char delimiters[] = ",!-;."; char *token = strtok(str, delimiters); while (token != nullptr) { std::cout << token << std::endl; token = strtok(nullptr, delimiters); } return 0; }
|