C++之string

来源:互联网 发布:如何查看淘宝联盟pid 编辑:程序博客网 时间:2024/05/16 14:33

在C++中我们用string来代替在C语言中char类型很多单操作的繁琐。
初始化string对象的方式:
string初始化

string的常用操作:
string常用操作

注意:

string str="Hello"+"World"//双引号引起的字符串通过加号链接是非法的

下面以一段代码来体会一下string的快感

/*题目描述:    1.提示用户输入姓名    2.接收用户的输入    3.然后向用户问好,hello xxx    4.告诉用户名字的长度    5.告诉用户名字的首字母    6.如果用户直接输入回车,那么告诉用户的输入为空    7.如果用户输入的是imooc,则告诉用户是一个管理员*/#include<iostream>#include<string>using namespace std;int main(){    string name;    cout << "Please input your name" << endl;    getline(cin, name);//如果直接用cin,那么回车会被保存,getline能将输入储存在name中    if (name.empty()) {        cout << "input is null.." << endl;        system("pause");        return 0;    }    if (name=="imooc") {        cout << "You are a adminitrator" << endl;    }    cout << "hello" + name << endl;    cout << "your name length is" <<name.size() << endl;    cout << "your name first letter is" << name[0] << endl;    system("pause");    return 0;}
0 0