typedef basic_string <char> string;
basic_string 此處可以不必深究。string s1(); // si = "" string s2("Hello"); // s2 = "Hello" string s3(4, 'K'); // s3 = "KKKK" string s4("12345", 1, 3); //s4 = "234",即 "12345" 的從下標 1 開始,長度為 3 的子串為稱呼方便,本教學後文將從字串下標 n 開始、長度為 m 的字串稱為“子串(n, m)”。
string s1('K'); string s2(123);
string s1; s1 = "Hello"; // s1 = "Hello" s2 = 'K'; // s2 = "K”string 類還有 assign 成員函數,可以用來對 string 物件賦值。assign 成員函數返回物件自身的參照。例如:
string s1("12345"), s2; s3.assign(s1); // s3 = s1 s2.assign(s1, 1, 2); // s2 = "23",即 s1 的子串(1, 2) s2.assign(4, 'K'); // s2 = "KKKK" s2.assign("abcde", 2, 3); // s2 = "cde",即 "abcde" 的子串(2, 3)
+
和+=
運算子對 string 物件執行字串的連線操作外,string 類還有 append 成員函數,可以用來向字串後面新增內容。append 成員函數返回物件自身的參照。例如:
string s1("123"), s2("abc"); s1.append(s2); // s1 = "123abc" s1.append(s2, 1, 2); // s1 = "123abcbc" s1.append(3, 'K'); // s1 = "123abcbcKKK" s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",新增 "ABCDE" 的子串(2, 3)
string s1("hello"), s2("hello, world"); int n = s1.compare(s2); n = s1.compare(1, 2, s2, 0, 3); //比較s1的子串 (1,2) 和s2的子串 (0,3) n = s1.compare(0, 2, s2); // 比較s1的子串 (0,2) 和 s2 n = s1.compare("Hello"); n = s1.compare(1, 2, "Hello"); //比較 s1 的子串(1,2)和"Hello” n = s1.compare(1, 2, "Hello", 1, 2); //比較 s1 的子串(1,2)和 "Hello" 的子串(1,2)
string substr(int n = 0, int m = string::npos) const;
呼叫時,如果省略 m 或 m 超過了字串的長度,則求出來的子串就是從下標 n 開始一直到字串結束的部分。例如:string s1 = "this is ok"; string s2 = s1.substr(2, 4); // s2 = "is i" s2 = s1.substr(2); // s2 = "is is ok"
string s1("West”), s2("East"); s1.swap(s2); // s1 = "East",s2 = "West"
#include <iostream> #include <string> using namespace std; int main() { string s1("Source Code"); int n; if ((n = s1.find('u')) != string::npos) //查詢 u 出現的位置 cout << "1) " << n << "," << s1.substr(n) << endl; //輸出 l)2,urce Code if ((n = s1.find("Source", 3)) == string::npos) //從下標3開始查詢"Source",找不到 cout << "2) " << "Not Found" << endl; //輸出 2) Not Found if ((n = s1.find("Co")) != string::npos) //查詢子串"Co"。能找到,返回"Co"的位置 cout << "3) " << n << ", " << s1.substr(n) << endl; //輸出 3) 7, Code if ((n = s1.find_first_of("ceo")) != string::npos) //查詢第一次出現或 'c'、'e'或'o'的位置 cout << "4) " << n << ", " << s1.substr(n) << endl; //輸出 4) l, ource Code if ((n = s1.find_last_of('e')) != string::npos) //查詢最後一個 'e' 的位置 cout << "5) " << n << ", " << s1.substr(n) << endl; //輸出 5) 10, e if ((n = s1.find_first_not_of("eou", 1)) != string::npos) //從下標1開始查詢第一次出現非 'e'、'o' 或 'u' 字元的位置 cout << "6) " << n << ", " << s1.substr(n) << endl; //輸出 6) 3, rce Code return 0; }
string s1("Real Steel"); s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替換 s1 的子串(1,3) cout << s1 << endl; //輸出 R3456 Steel string s2("Harry Potter"); s2.replace(2, 3, 5, '0'); //用 5 個 '0' 替換子串(2,3) cout << s2 << endl; //輸出 HaOOOOO Potter int n = s2.find("OOOOO"); //查詢子串 "00000" 的位置,n=2 s2.replace(n, 5, "XXX"); //將子串(n,5)替換為"XXX" cout << s2 < < endl; //輸出 HaXXX Potter
string s1("Real Steel"); s1.erase(1, 3); //刪除子串(1, 3),此後 s1 = "R Steel" s1.erase(5); //刪除下標5及其後面的所有字元,此後 s1 = "R Ste"
string s1("Limitless"), s2("00"); s1.insert(2, "123"); //在下標 2 處插入字串"123",s1 = "Li123mitless" s1.insert(3, s2); //在下標 2 處插入 s2 , s1 = "Li10023mitless" s1.insert(3, 5, 'X'); //在下標 3 處插入 5 個 'X',s1 = "Li1XXXXX0023mitless"
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string src("Avatar 123 5.2 Titanic K"); istringstream istrStream(src); //建立src到istrStream的聯絡 string s1, s2; int n; double d; char c; istrStream >> s1 >> n >> d >> s2 >> c; //把src的內容當做輸入流進行讀取 ostringstream ostrStream; ostrStream << s1 << endl << s2 << endl << n << endl << d << endl << c <<endl; cout << ostrStream.str(); return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string s("afgcbed"); string::iterator p = find(s.begin(), s.end(), 'c'); if (p!= s.end()) cout << p - s.begin() << endl; //輸出 3 sort(s.begin(), s.end()); cout << s << endl; //輸出 abcdefg next_permutation(s.begin(), s.end()); cout << s << endl; //輸出 abcdegf return 0; }