1 // 13-字符串基于string.cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <climits>
7 #include <string> //引入string类库
8 using namespace std;
9
10
11 int main()
12 {
13 string str1;
14 string str2 = "wwww.uimodel.com";
15 cout << str1 << endl;
16 cout << str2 << endl;
17 str1 = str2;//相比较C语言,C++引入string类库后,就能对string变量赋值。
18 cout << str1 << endl;
19
20 cout << "请输入一段话: ";
21 //cin >> str1; //如果使用cin接收用户输入,输入信息中不允许有空白。
22 getline(cin,str1);//可以使用getline()方法来接收整行的信息。
23 cout << str1 << endl;
24
25 cout << str2[5] << endl;//访问C++中string类本质上还是访问字符数组,所以可以通过字符数组索引(下标)来访问数组中的值。
26
27 //string s="uimodel"+".com"; //这个语法是错误的,因为"uimodel"和".com"表示的不是string类型。
28 str1 = "uimodel"; //所以先将它们都定义成string类型后在相加。
29 str2 = ".com";
30 string s = str1 + str2;
31 s += str1;
32 cout << s << endl;
33
34 //获取字符的个数
35 cout << s.size() << endl;
36
37 int t;
38 cin >> t;
39 return 0;
40 }