字符串类型
2015-09-14 21:30
288 查看
1.字符数组
[code]char str[10]; cin>>str;
2.字符指针
[code]char *str=new char[10]; cin>>str
这里的str跟字符数组中的str含义是相同的。如果不写new char[10]则程序错误,必须分配空间。
3.String类型
String类是C++的一个模板类,使用string类要包含头文件string。string对象的初始化
[code]string s1("Hello"); string s2(8,'x'); string month = "March"
不提供以字符和整数为参数的构造函数,以下是错误的初始化方法:
[code]string error1 = 'c'; string error2('u'); string error3 = 22; string error4(8);
若要将字符赋值给string对象,要用以下形式:
[code]string s; s = 'n';
成员函数
string对象的长度用成员函数length()读取[code]string s("hello"); cout<<s.length()<<endl;
string支持流读取运算符
[code]string stringObject; cin>>stringObject;
string支持getline()函数
[code]string s; getline(cin,s);
string的赋值和连接
用’=’赋值:[code]string s1("cat"),s2; s2 = s1;
用assign()成员函数复制:
[code]string s1("cat"),s3; s3.assign(s1);
用assign()成员函数部分复制:
[code]string s1("catpig"),s3; s3.assign(s1,1,3);//从s1中下标为1的字符开始复制3个字符给s3
单个字符复制:
[code]s2[5] = s1[3] = 'a';
逐个访问string对象中的字符
[code]string s1("Hello"); for(int i=0;i<s1.length();i++) cout<<s1.at(i)<<endl;
成员函数at()会做范围检查,如果超出范围,会抛出out_of_range的异常,下标运算符不做范围检查。
用+运算符连接字符串:
[code]string s1("good "),s2("morning!"); s1 += s2; cout<<s1;
用成员函数append()连接字符串:
[code]string s1("good "),s2("morning!"); s1.append(s2); cout<<s1; s2.append(s1,3,s1.size());//s1.size()是s1的字符数 cout<<s2; //下标从3开始,s1.size()个字符 //如果字符串内没有足够字符,则复制到字符串最后一个字符
比较string
用关系运算符(==,>,>=,<,<=,!=)比较string的大小,返回值都是bool类型,成立返回true(1),否则返回false(0)。用成员函数compare比较string的大小:
[code]int f = s1.compare(s2); cout << f <<endl; //若s1==s2则返回0,若s1<s2则返回-1,否则返回1
子串:成员函数substr()
[code]string s1("hello world"),s2; s2 = s1.substr(4,5);//下标从4开始的5个字符 cout<<s2<<endl;//输出o wor
交换string:成员函数swap()
[code]string s1("hello world"),s2("really") s1.swap(s2); cout<<s1<<endl; cout<<s2<<endl; //交换s1和s2的内容
string的特性
成员函数capacity():返回无需增加内存即可存放的字符数成员函数maximum_size():返回string对象可存放的最大字符数
成员函数length()和size():返回字符串的长度
成员函数empty():返回string对象是否为空
成员函数resize():改变string对象的长度
寻找string中的字符
(1)成员函数find()[code]string s1("hello world"); s1.find("lo");
在s1中从前向后查找”lo”第一次出现的位置,如果找到则返回”l”所在的位置下标;如果找不到则返回string::npos
(2)成员函数rfind()
[code]string s1("hello world"); s1.rfind("lo");
在s1中从后向前查找”lo”第一次出现的位置,如果找到则返回”l”所在的位置下标;如果找不到则返回string::npos
(3)成员函数find_first_of()
[code]string s1("hello world"); s1.find_first_of("abcd");
在s1中从前向后查找”abcd”中任何一个字符第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(4)成员函数find_last_of()
[code]string s1("hello world"); s1.find_last_of("abcd");
在s1中查找”abcd”中任何一个字符最后一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(5)成员函数find_first_not_of()
[code]string s1("hello world"); s1.find_first_not_of("abcd");
在s1中从前往后查找不在”abcd”中的字母第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(6)成员函数find_last_not_of()
[code]string s1("hello world"); s1.find_last_not_of("abcd");
在s1中从后往前查找不在”abcd”中的字母第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
替换string中的字符
(1)成员函数erase()[code]string s1("hello world"); s1.erase(5);//去掉下标5及之后的字符 cout<<s1; cout<<s1.length(); cout<<s1.size(); //输出:hello55
(2)成员函数replace()
[code]string s1("hello world"); s1.replace(2,3,"haha");//将s1中下标2开始的3个字符换成"haha" cout<<s1; //输出:hehaha world
[code]string s1("hello world"); s1.replace(2,3,"haha",1,2);//将s1中下标2开始的3个字符换成"haha"中下标1开始的2个字符 cout<<s1; //输出:heah world
在string中插入字符
成员函数insert()[code]string s1("hello world"); string s2("show insert"); s1.insert(5,s2);//将s2插入s1下标5的位置 cout<<s1<<endl; s1.insert(2,s2,5,3);//将s2中下标5开始的3个字符插入s1下标2的位置 cout<<s1<<endl; //输出: //helloshow insert world //heinslloshow insert world
转换成C语言式char *字符串
(1)成员函数c_str()[code]string s1("hello world"); printf("%s\n",s1.c_str()); //s1.c_str()返回传统的const char*类型字符串,且该字符串以'\0'结尾 //输出:hello world
(2)成员函数data()
[code]string s1("hello world"); const char* p1=s1.data(); for(int i=0;i<s1.length();i++) printf("%c",*(p1+i)); //s1.data()返回一个char*类型的字符串 //输出:hello world
(3)成员函数copy()
[code]string s1("hello world"); int len=s1.length; char* p2=new char[len+1]; s1.copy(p2,5,0); p2[5]=0; cout<<p2<<endl; //s1.copy(p2,5,0)从s1的下标0的字符开始的5个字符长度的字符串副本赋值给p2 //输出:hello
相关文章推荐
- Android获取手机最近任务列表
- 5、shader混合(Blending)、雾
- java网店系统JSPGOU 4.5
- HDU 4302 Holedox Eating (multiset模拟)
- StringBuffer
- 利用Spring创建定时任务
- android实现沉浸式状态栏
- 设置系统环境变量需要重启吗?
- 程序包管理
- Linux指令学习笔记(一)
- 乐观锁和悲观锁
- [疑似官泄] 某Ri牌示波器固件破解
- Java web访问MySql数据库分页查询
- 宏定义以及例子
- 揭秘云计算 为应用程序选择最佳云计算平台
- 使用DOSBox在Win7_x64下搭建汇编环境
- StateListDrawable(状态)资源属性
- java虚拟机的面试问题
- centos 7 防火墙和端口配置
- 用AXIS2发布法WebService的方法