string类函数

string类程序样例
string s1(“hello”);
cout<<s1; //输出hello
string s2(8,‘x’);
cout<<s2; //输出xxxxxxxx
string month = “march”;
cout<<moth<<endl; //输出march
string s;
s=‘n’;
cout<<s<<endl; //输出n
string c;
cin>>c;
cout<<c;// string输入输出可用cin,cout

string的赋值和连接
string s1(“cat”),s2;
s2=s1;//将s1赋值给s2
用assign成员函数复制
string s1(“cat”),s3;
s3.assign(s1); //此时s3为cat
用assign成员函数部分复制
string s1(“cating”),s3;
s3.assign(s1,1,3);//从s1中下标为1的字符开始复制3个字符,此时s3为ati
单个字符的复制
s2[5]=s1[3]=‘a’;
逐个访问sting对象中的字符
string s1(“hello”);
for(int i=0;i<s1.length();i++)
cout<<s1.at(i);//成员函数at会做范围检查,如果超出范围,会抛出异常,而下标运算符不做范围检查
用+运算符连接字符串
string s1(“good”),s2(“morning”);
s1+=s2;//此时s1为good morning
用成员函数append连接字符串
string s1(“good”),s2(“morning”);
s1.append(s2);
cout<<s1;//此时s1为good morning
s2.append(s1,3,s1.size());//此时s2为morningd,下标为3开始,s1.size()个字符,如果字符串内没有足够字符,则复制到字符串最后一个字符

比较string
用关系运算符比较string的大小
==,>,>=,<,<=,!=
返回值都是bool类型,成立返回true,否则返回false
例如
string s1(“hello”),s2(“hello”),s3(“hell”);
bool b=(s1 == s2);
cout<<b<<endl;
b=(s1 == s3);
cout<<b<<endl;
b=(s1>s3);
cout<<b<<endl;
输出:
1
0
1

用成员函数compare比较string的大小
string s1(“hello”),s2(“hello”),s3(“hell”);
int f1=s1.compare(s2);
int f2=s1.compare(s3);
int f3=s3.compare(s1);
int f4=s1.compare(1,2,s3,0,3);//s1从下标为1开始的2个字母与s3下标为0开始的3个字母比较
int f5=s1.compare(0,s1.size(),s3);
cout<<f1<<endl<<f2<<endl<<f3<<enfl<<f4<<endl<<f5;
输出:
0
1
-1
-1
1
//等于输出0,大于输出1,小于输出-1;

子串
成员函数 substr
string s1(“hello world”),s2;
s2=s1.substr(4,5);//从下标4开始的5个字符,若超过字符串长度则输出到最后一个字符为止
cout<<s2<<endl;
输出: o wor

交换string
成员函数 swap
s1.swap(s2);//交换s1和s2

寻找string中的字符串

成员函数find()
string s1(“hello world”);
s1.find(“lo”);
在s1中从前向后查找“lo”第一次出现的地方,如果找到,返回“lo”开始的位置,即l所在的位置下标。如果找不到,返回string::npos(string中定义的静态常量)

成员函数rfind()
string s1(“hello world”);
s1.rfind(“lo”);
在s1中从后向前查找“lo”第一次出现的地方,如果找到,返回“lo”开始的位置,即l所在的位置下标。如果找不到,返回string::npos(string中定义的静态常量)
string s1(“hello worlld”)
s1.find(“ll”,1);
s1.find(“ll”,2);
s1.find(“ll”,3);//分别从下标1,2,3开始查找
输出 2
2
9//不是10

成员函数 find_first_of()
string s1(“hello world”);
s1. find_first_of(“abcd”);//输出10
在s1中从前向后查找“abcd”中任何一个字符第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos

成员函数 find_last_of()
string s1(“hello world”)
s1. find_last_of(“abcd”);//输出10
在s1中查找“abcd”中任何一个字符最后一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos

成员函数 find_first_not_of()
string s1(“hello world”);
s1. find_first_not_of(“abcd”);//输出0
在s1中从前向后查找不在“abcd”中的字母第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos

成员函数 find_last_not_of()
string s1(“hello world”);
s1. find_first_not_of(“abcd”);//输出9
在s1中从后向前查找不在“abcd”中的字母第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos

成员函数erase()
string s1(“hello world”);
s1.erase(5);//删除下标5及其之后的字符
cout<<s1<<endl<<s1.length()’
输出:hello
5

成员函数replace()
string s1(“hello world”);
s1.repalce(2,3"haha");//将s1中下标2开始的3个字符换成“haha”
cout<<s1;
输出:
hehaha world

在string中插入字符
成员函数insert()
string s1(“hello world”);
string s2(“show insert”);
s1.insert(5,s2);//将s2插入s1下标5的位置
cout<<s1<<endl;//输出 helloshow insert world
s1.insert(2,s2,5,3);//将s2中5下边开始的3个字符插入s1下标2的位置
cout<<s1<<endl;//输出 heinslloshow insert world

转换成c语言式char *字符串
成员函数c_str()
string s1(“abc”);
printf("%s\n",s1.c_str());//s1.c_str()返回传统的const char *类型字符串,且该字符串以’\0’结尾

成员函数data()
string s1(“hello world”);
const char p1=s1.data();
for(int i=0;i<s1.length();i++)
printf("%c",
(p1+i));//输出hello world
//s1.data()返回一个char *类型的字符串,对s1的修改可能会使p1出错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值