See More

// string.cpp : ¶¨Òå¿ØÖÆÌ¨Ó¦ÓóÌÐòµÄÈë¿Úµã¡£ // ±ê×¼C++ÖÐstringÀ࣬±ØÐëÒª#includeºÍusing namespace std; // È»ºó¾Í¿ÉÒÔÓÃstring/wstringÁË£¬ËüÃÇÁ½·Ö±ð¶ÔÓ¦×ÅcharºÍwchar_t // Ö÷Òª²Î¿¼ÎÄÏ× // 1¡¢±ê×¼C++ÖеÄstringÀàµÄÓ÷¨×ܽᣨת£© // http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html #include "stdafx.h" #include #include #include #include using namespace std; /* ³õʼ»¯±äÁ¿demo */ void InitialDemo() { string str1(5, 'c');//³õʼ»¯Îª5¸ö'c' string str2("Now is the time..."); string str3(str2, 11, 4);//´Óstr2µÄµÚ11¸ö×Ö·û¿ªÊ¼£¬¶ÁÈ¡4¸ö×Ö·û string str4; str4.append(3, 'h');//Ôö¼Ó3¸ö'h' char chr[6]; memset(chr, '\0', sizeof(chr)); chr[0] = '2'; string str5(chr); cout << endl << "InitialDemo:" << endl; cout << str1 << endl; if(str1[5]== '\0') { cout << "Ô½½çÊä³ö£ºstr1[5]=\0" << endl;//¿Õ } else { cout << "Ô½½çÊä³ö£ºstr1[5]=" << str1[5] << endl;//¿Õ } //cout << "Ô½½çÊä³ö£ºstr1[6]=" << str1[6] << endl;//Òì³£ cout << str2 << endl; cout << str3 << endl; cout << str4 << endl; cout << str5 << endl; } /* ¸³ÖµºÍÌæ»»demo */ void AssignReplaceDemo() { cout << endl << "AssignReplaceDemo:" << endl; string str1, str2 = "War and Peace"; str1.assign(str2, 4, 3); cout << str1 << endl; string s = "They say he carved it himself...from a BIGGER spoon"; string s2 = "find your soul-mate, Homer."; //s[1] = "1";²»ÄÜÓÃÕâÖÖÐÎʽ //replace(off,number,new string) s.replace(5, s2.length(), s2); //char* p;char c = s[0];p = &c; //s.replace(5, 1, p); //½á¹ûΪ"They say he carved it himself...find your soul-mate, Homer." cout << s << endl; } string ReverseStringDemo(string s) { int left = 0, right = s.size() - 1; while (left < right) { char t = s[left]; s[left++] = s[right]; s[right--] = t; } return s; } string ReverseStringDemo2(string s) { int left = 0, right = s.size() - 1; while (left < right) { swap(s[left++], s[right--]); } return s; } void Change2CharDemo() { cout << endl << "Change2CharDemo:" << endl; string str1 = "War and Peace"; const char* chr = str1.c_str(); cout << chr << endl; } void Change2IntDemo() { string str1 = "-20"; string str2 = "5"; int ret = stoi(str1) + stoi(str2); cout << "string to int: -20+5=" << ret << " " << endl; int a = -3, b = 37; string a_plus_b = to_string(a + b); cout << "int to string: -3+37=" << a_plus_b << " " << endl; string str3 = "-0x12"; string str4 = "0x76E";//1902 char *offset; cout << "string to int: -0x12=" << strtol(str3.c_str(),&offset,0) << " " << endl; cout << "string to int: 0x76E=" << strtol(str4.c_str(), &offset, 0) << " " << endl; cout << "string to int: 0x76E=" << stol(str4, nullptr,0) << " " << endl; string str5 = "A4"; char *offset5; int istr5 = strtol(str5.c_str(), &offset5, 10); cout << "string to int: A4=" << istr5 << " " << endl;//0 } void SubStringDemo() { string str = "20+-22i"; int size = str.size(); int plus = str.find("+"); string real = str.substr(0, plus);//off,count string imaginary = str.substr(plus + 1, size - plus - 2); cout << "substring: " << real << " " << imaginary << endl; } void FindDemo() { string str; cout << "FindDemo: please cin words and find spaces "<