C++string的操作

  1 #include <iostream>
  2 using namespace std;
  3 
  4 
  5 int main()
  6 {
  7     //initilization
  8     string            str("abc.ddd");
  9     const    string     cstr("fff.ccc");
 10     string substr1(str, 2); //c.ddd
 11     string substr2(str, 2, 3); //c.d
 12     string substr3("abcdefg", 2); //ab
 13     cout << "the value of substr1 is: " << substr1 << endl;
 14     cout << "the value of substr2 is: " << substr2 << endl;
 15     cout << "the value of substr3 is: " << substr3 << endl;
 16 
 17     //compare
 18     if(str > cstr)
 19         cout << "abc.ddd is larger than fff.ccc." << endl;
 20     else
 21         cout << "abc.ddd is less than fff.ccc." << endl;
 22     if(str.compare(cstr) > 0)
 23         cout << "abc.ddd is larger than fff.ccc." << endl;
 24     else
 25         cout << "abc.ddd is less than fff.ccc." << endl;
 26 
 27     //assign
 28     str = "sadfasdf";
 29     str.assign("a",5); //a,,,,
 30     str.assign(5,'a'); //a, a, a, a, a
 31     cout << "the value of str is: " << str << endl; //aaaaa
 32 
 33     //append
 34     str.append("bcd");
 35     cout << "the value of s after append is: " << str << endl; //aaaaabcd
 36     
 37     //insert
 38     //s.insert(1, 'd'); NOK!
 39     str.insert(1, "ddd"); //adddaaaabcd
 40     cout << "the value of s after append is: " << str << endl;
 41     
 42     //find
 43     string::size_type idx = str.find(".");
 44     cout << "the index of . is: " << idx << endl; //3
 45 
 46     //substring
 47     string basestr = str.substr(0, idx); //abc
 48     string extestr = str.substr(idx + 1, string::npos); //ddd
 49     cout << "the substr of str.substr(0, idx) is: " << basestr << endl;
 50     cout << "the substr of str.substr(idx, string::npos) is: " << extestr << endl;
 51 
 52     //compare
 53     if(extestr == "ddd")
 54         cout << "ddd file is found!" << endl;
 55     else
 56         cout << "ddd file is not found!" << endl;
 57     
 58     //replace
 59     string tmpname(str.replace(idx + 1, string::npos, "xxx")); //abc.xxx
 60     cout << "the string after replace extention is: " << tmpname << endl;
 61 
 62     //erase
 63     str = "internal";
 64     str.replace(0,2,"ex"); //external
 65     str.erase(5);
 66     str.erase(2,2);
 67     cout << "the string after erase is: " << str << endl;
 68 
 69     //clear
 70     str.clear();
 71     cout << "the string after clear is: " << str << endl;
 72     if(str.empty())
 73         cout << "the string is empty." << endl;
 74     else
 75         cout << "the string is not empty." << endl;
 76     if(str.begin() == str.end())
 77         cout << "equal." << endl;
 78     else
 79         cout << "unequal" << endl;
 80 
 81     //reverse
 82     str = "abcd";
 83     reverse(str.begin(), str.end());
 84     cout << "the string after reverse is: " << str << endl;
 85     str.assign(str.rbegin(), str.rend());
 86     cout << "the string after reverse is: " << str << endl;
 87 
 88     //data
 89     const char* pa = str.data();
 90 
 91     //size(), length()
 92     cout << "the size of str is: " << str.size() << endl;
 93     cout << "the size of str is: " << str.length() << endl;
 94 
 95     //[], at()
 96     char& r = str[2];
 97     char* p = &str[3];
 98     cout << "the 3rd char of str is: " << r << endl;
 99     cout << "the 4rd char of str is: " << *p << endl;
100     str = "new value";
101     //reference is invalid after str is re-assigned
102     r = 'X';
103     cout << "The value of str is: " << str << endl;
104 
105     //advanced find    
106     //Input: I was a deer
107     //Output: reed a saw I
108     const string delims(" 	,.;");
109     string line;
110     cout << "Please input a sentence: " << endl;
111     getline(cin, line,'
');
112     cout << "The input sentence is: " << line << endl;
113     //while find a word
114     string::size_type begIdx, endIdx;
115     begIdx = line.find_first_not_of(delims);
116     while(begIdx != string::npos){
117         endIdx = line.find_first_of(delims, begIdx); 
118         if(endIdx == string::npos);
119             //endIdx = line.length();
120         for(int i = endIdx - 1; i >= static_cast<int>(begIdx); --i)
121             cout << line[i];
122         cout << ' '; 
123         begIdx = line.find_first_not_of(delims, endIdx);
124 
125     }
126     cout << endl;
127 
128 }
原文地址:https://www.cnblogs.com/dracohan/p/3768460.html