String Class 2 String Class Constructors 3 String

  • Slides: 18
Download presentation
字串類別 (String Class)

字串類別 (String Class)

2 String Class Constructors

2 String Class Constructors

3 String Class Overloaded Operatorsa (1/2) char My. Old. String[25] = “Howdy Partner”;

3 String Class Overloaded Operatorsa (1/2) char My. Old. String[25] = “Howdy Partner”;

#include <iostream> #include <string> using namespace std; int main() { string S 1("What a

#include <iostream> #include <string> using namespace std; int main() { string S 1("What a great day. "), S 2, S 3; char My. Old. String[25] = "Howdy Partner"; string S 4(My. Old. String); S 2 = "Do you love C++? "; //assignment S 3 = S 1 + S 2; //Write cout << cout << the initial string values "n S 1 = " << S 1; "n S 2 = " << S 2; "n S 3 = " << S 3; "n S 4 = " << S 4; "n My. Old. String = " << My. Old. String << endl; if (S 1 > S 2) cout << "n S 1 is greater than S 2"; else cout << "n S 1 is not greater than or the same as S 2"; S 1 = "n. All done with strings. "; cout << S 1 << endl; } return 0; 5

6

6

7 String Editing Functions 常用的編輯字串指令 應改成 8

7 String Editing Functions 常用的編輯字串指令 應改成 8

// Program: String Class Example using Editing Functions #include <iostream> #include <string> using namespace

// Program: String Class Example using Editing Functions #include <iostream> #include <string> using namespace std; int main() { string S 1("It is a rainy day. "); string S 2("very "); string S 3("sunny "); //Write the initial string values cout << "n S 1 = " << S 1; cout << "n S 2 = " << S 2; cout << "n S 3 = " << S 3; // Insert "very " into S 1. insert(8, S 2); cout << "n S 1 = " << S 1; // Replace "rainy" with "sunny" in S 1. replace(13, 6, S 3); cout << "n S 1 = " << S 1; // Erase "very" from S 1. erase(8, 5); cout << "n S 1 = " << S 1; S 1 = "n. All done with strings. "; cout << S 1 << endl; } return 0; 8

9

9

10 String Searching Functions 常用的字串搜尋指令

10 String Searching Functions 常用的字串搜尋指令

#include <iostream> #include <string> using namespace std; int main() { string S 1("One potato,

#include <iostream> #include <string> using namespace std; int main() { string S 1("One potato, two potatoes, three potatoes, four. "); string S 2("potato"); int First_pos, Last_pos; //Write the initial string values cout << "n S 1 = " << S 1; cout << "n S 2 = " << S 2; // Search S 1 for "potato" from start of the string First_pos = S 1. find(S 2, 0); cout << "n The first "potato" is at " << First_pos; // string: : npos is defined as the length of the string Last_pos = S 1. rfind(S 2, string: : npos); cout << "n The last "potato" is at " << Last_pos; S 1 = "n. All done with strings. "; cout << S 1 << endl; return 0; } 11

12

12

13 Miscellaneous String Functions (1/2)

13 Miscellaneous String Functions (1/2)

14 Miscellaneous String Functions (2/2)

14 Miscellaneous String Functions (2/2)

15 Conversion between Char Arrays and Strings // Program: Convert Between Strings and Character

15 Conversion between Char Arrays and Strings // Program: Convert Between Strings and Character String Arrays #include <iostream> //C++ header style since using C++ string class #include <string> using namespace std; int main() { string S 1("I like the sea and C, you see. "); string S 2; char My. Array[50] = "Is C++ really a B-? "; const char *My. Other. Array; cout << "n The original string and array" << endl; cout << S 1 << endl; cout << My. Array << endl; //First we place My. Array contents into S 2 using the assign S 2. assign(My. Array); //Next, we'll stuff the contents of S 1 into My. Other. Array = S 1. c_str(); //convert a string to a const char* cout << "n The converted array and string" << endl; cout << My. Other. Array << endl; cout << S 2 << endl; return 0; }

Reading into a String (e. g. , from the Keyboard) • To read text

Reading into a String (e. g. , from the Keyboard) • To read text with whitespace characters – Use get or getline and assign the contents into a string #include <iostream> #include <string> using namespace std; int main() { string S 1; char My. Array[50]; cout << "n Enter your favorite string saying " << endl; cin. getline(My. Array, 50); //Assign the array contents into S 1. assign(My. Array); cout << "n Here is your saying twice! "<< endl; cout << My. Array << endl; cout << S 1 << endl; return 0; } 16