1.字符数组
char str[10];
cin>>str;
2.字符指针
char *str=new char[10];
cin>>str
这里的str跟字符数组中的str含义是相同的。如果不写new char[10]则程序错误,必须分配空间。
3.String类型
String类是C++的一个模板类,使用string类要包含头文件string。
string对象的初始化
string s1("Hello");
string s2(8,'x');
string month = "March"
不提供以字符和整数为参数的构造函数,以下是错误的初始化方法:
string error1 = 'c';
string error2('u');
string error3 = 22;
string error4(8);
若要将字符赋值给string对象,要用以下形式:
string s;
s = 'n';
成员函数
string对象的长度用成员函数length()读取
string s("hello");
cout<<s.length()<<endl;
string支持流读取运算符
string stringObject;
cin>>stringObject;
string支持getline()函数
string s;
getline(cin,s);
string的赋值和连接
用’=’赋值:
string s1("cat"),s2;
s2 = s1;
用assign()成员函数复制:
string s1("cat"),s3;
s3.assign(s1);
用assign()成员函数部分复制:
string s1("catpig"),s3;
s3.assign(s1,1,3);//从s1中下标为1的字符开始复制3个字符给s3
单个字符复制:
s2[5] = s1[3] = 'a';
逐个访问string对象中的字符
string s1("Hello");
for(int i=0;i<s1.length();i++)
cout<<s1.at(i)<<endl;
成员函数at()会做范围检查,如果超出范围,会抛出out_of_range的异常,下标运算符不做范围检查。
用+运算符连接字符串:
string s1("good "),s2("morning!");
s1 += s2;
cout<<s1;
用成员函数append()连接字符串:
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的大小:
int f = s1.compare(s2);
cout << f <<endl;
//若s1==s2则返回0,若s1<s2则返回-1,否则返回1
子串:成员函数substr()
string s1("hello world"),s2;
s2 = s1.substr(4,5);//下标从4开始的5个字符
cout<<s2<<endl;//输出o wor
交换string:成员函数swap()
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()
string s1("hello world");
s1.find("lo");
在s1中从前向后查找”lo”第一次出现的位置,如果找到则返回”l”所在的位置下标;如果找不到则返回string::npos
(2)成员函数rfind()
string s1("hello world");
s1.rfind("lo");
在s1中从后向前查找”lo”第一次出现的位置,如果找到则返回”l”所在的位置下标;如果找不到则返回string::npos
(3)成员函数find_first_of()
string s1("hello world");
s1.find_first_of("abcd");
在s1中从前向后查找”abcd”中任何一个字符第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(4)成员函数find_last_of()
string s1("hello world");
s1.find_last_of("abcd");
在s1中查找”abcd”中任何一个字符最后一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(5)成员函数find_first_not_of()
string s1("hello world");
s1.find_first_not_of("abcd");
在s1中从前往后查找不在”abcd”中的字母第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
(6)成员函数find_last_not_of()
string s1("hello world");
s1.find_last_not_of("abcd");
在s1中从后往前查找不在”abcd”中的字母第一次出现的位置,如果找到则返回该字母所在的位置下标;如果找不到则返回string::npos
替换string中的字符
(1)成员函数erase()
string s1("hello world");
s1.erase(5);//去掉下标5及之后的字符
cout<<s1;
cout<<s1.length();
cout<<s1.size();
//输出:hello55
(2)成员函数replace()
string s1("hello world");
s1.replace(2,3,"haha");//将s1中下标2开始的3个字符换成"haha"
cout<<s1;
//输出:hehaha world
string s1("hello world");
s1.replace(2,3,"haha",1,2);//将s1中下标2开始的3个字符换成"haha"中下标1开始的2个字符
cout<<s1;
//输出:heah world
在string中插入字符
成员函数insert()
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()
string s1("hello world");
printf("%s\n",s1.c_str());
//s1.c_str()返回传统的const char*类型字符串,且该字符串以'\0'结尾
//输出:hello world
(2)成员函数data()
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()
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