标签:
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <string.h> 5 using namespace std; 6 7 int main() 8 { 9 string s(4,‘x‘); 10 cout << s << endl;//xxxx 11 12 s += "xx"; 13 14 string buffer = "E:\\1.txt"; 15 ifstream infile; 16 infile.open(buffer.c_str());//返回 17 18 //erase 19 string s1 = "This is a test string"; 20 s1.erase(10, 5); 21 cout << s1 << endl;//This is a string 22 23 //Insert 24 string s2 = "test "; 25 s1.insert(10, s2); 26 cout << s1 << endl;//This is a test string 27 28 //replace 29 s1 = "This is a string"; 30 s1.replace(10, 6, s2); 31 cout << s1 << endl;//This is a test 32 33 //swap 34 s1.swap(s2); 35 cout << s1 << endl; // test 36 37 s2[2] = ‘a‘; s2[3] = ‘t‘; 38 cout << s2 << endl;//That is a test 39 40 //substr 41 s1 = s2.substr(10, 4); 42 cout << s1 << endl;//test 43 44 //find(子串,初始查找位置) 45 cout << s2.find("is", 0) << endl;//5 46 cout << s2.find("at", 0) << endl;//2 47 48 //rfind(子串,初始查找位置)从后往前找 49 cout << s2.rfind("a", 13) << endl;//8 50 cout << s2.rfind("t", 13) << endl;//13 51 52 //find_first_of,find_first_not_of 53 //find_last_of,find_last_not_of 54 s1 = "abcdef"; 55 cout << s1.find_first_of("xeyz") << endl;//4 56 cout << s1.find_first_not_of("xyba") << endl;//2 57 cout << s1.find_last_of("xeyz") << endl;//4 58 cout << s1.find_last_not_of("xyba") << endl;//5 59 60 //string.h strlen, strcmp, strcpy, atoi, strtok, strcat 61 cout << strlen(s1.c_str()) << endl;//6 abcdef 62 63 //c_str()是const char* 64 char s3[500]; 65 strcpy(s3, s1.c_str()); 66 cout << s3 << endl;//abcdef 67 68 char s4[500]; 69 strcpy(s4, s2.c_str()); 70 cout << strcmp(s3, s3) << endl;//0 71 cout << strcmp(s3, s4) << endl;//1 72 73 cout << strcat(s3, s4) << endl;//abcdefThat is a test 74 75 strcpy(s3, "123"); 76 cout << atoi(s3) << endl;//123 77 78 s1 = "123 456,789,你好"; 79 strcpy(s3, s1.c_str()); 80 char *p = strtok(s3, " "); 81 while((p = strtok(NULL, ","))) 82 { 83 cout << p << endl; 84 /* 85 123 86 456 87 789 88 你好 89 */ 90 } 91 return 0; 92 }
标签:
原文地址:http://www.cnblogs.com/wanderingzj/p/5293199.html