C++有一個字符串類。
您可以使用類型字符串變量。
要使用字符串類,程序必須包括字符串頭文件。
字符串類是std命名空間的一部分。
該類可以讓您將字符串視為普通變量。
以下代碼說明了字符串對象和字符數(shù)組之間的一些相似之處和差異。
#include <iostream>
#include <string> // make string class available
using namespace std;
int main() {
char my_char1[20]; // create an empty array
char my_char2[20] = "C++"; // create an initialized array
string str1; // create an empty string object
string str2 = "Java"; // create an initialized string
cout << "Enter a string: ";
cin >> my_char1;
cout << "Enter another string: ";
cin >> str1; // use cin for input
cout << my_char1 << " " << my_char2 << " "
<< str1 << " " << str2 // use cout for output
<< endl;
cout << "The third letter in " << my_char2 << " is "
<< my_char2[2] << endl;
cout << "The third letter in " << str2 << " is "
<< str2[2] << endl; // use array notation
return 0;
}
上面的代碼生成以下結(jié)果。
以下代碼顯示了一些String用法。
請注意,您可以將C風(fēng)格的字符串以及字符串對象添加到字符串對象。
#include <iostream>
#include <string> // make string class available
using namespace std;
int main() {
string s1 = "www.15014759268.cn";
string s2, s3;
cout << "You can assign one string object to another: s2 = s1\n";
s2 = s1;
cout << "s1: " << s1 << ", s2: " << s2 << endl;
cout << "You can assign a C-style string to a string object.\n";
cout << "s2 = \"new Value\"\n";
s2 = "C++";
cout << "s2: " << s2 << endl;
cout << "You can concatenate strings: s3 = s1 + s2\n";
s3 = s1 + s2;
cout << "s3: " << s3 << endl;
cout << "You can append strings.\n";
s1 += s2;
cout <<"s1 += s2 yields s1 = " << s1 << endl;
s2 += " for a day";
cout <<"s2 += \" for a day\" yields s2 = " << s2 << endl;
return 0;
}
上面的代碼生成以下結(jié)果。
C庫相當(dāng)于
str3 = str1 + str2;
這是:
strcpy(my_char3, my_char1); strcat(my_char3, my_char2);
以下代碼將使用字符串對象的技術(shù)與使用字符數(shù)組的技術(shù)進(jìn)行比較。
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
using namespace std;
int main() {
char my_char1[20];
char my_char2[20] = "www.15014759268.cn";
string str1;
string str2 = "C++";
str1 = str2; // copy str2 to str1
strcpy(my_char1, my_char2); // copy my_char2 to my_char1
str1 += " vs "; // add to the end of str1
strcat(my_char1, " Java"); // add to the end of my_char1
int len1 = str1.size(); // obtain length of str1
int len2 = strlen(my_char1);// obtain length of my_char1
cout << "The string " << str1 << " contains "
<< len1 << " characters.\n";
cout << "The string " << my_char1 << " contains "
<< len2 << " characters.\n";
return 0;
}
上面的代碼生成以下結(jié)果。
您可以使用cin與>>操作符來讀取一個字符串對象,并使用<<操作符來顯示一個使用與C風(fēng)格字符串相同的語法的字符串對象。
一次讀取一行而不是一個字使用不同的語法。
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
using namespace std;
int main() {
char my_char[20];
string str;
cout << "Length of string: "
<< strlen(my_char) << endl;
cout << "Length of string in str before input: "
<< str.size() << endl;
cout << "Enter a line of text:\n";
cin.getline(my_char, 20); // indicate maximum length
cout << "You entered: " << my_char << endl;
cout << "Enter another line of text:\n";
getline(cin, str); // cin now an argument; no length specifier
cout << "You entered: " << str << endl;
cout << "Length of string in my_char after input: "
<< strlen(my_char) << endl;
cout << "Length of string in str after input: "
<< str.size() << endl;
return 0;
}
上面的代碼生成以下結(jié)果。
更多建議: