C++中对String的各种操作

1.string的各种动态、静态赋值

//静态
string s1=("abckd");
string s2("bbbbb");
string s3(s1);
string s4=s2;
//动态
string s5;
cin>>s5;

2.sting的遍历

string s="xiaojaiyu";
string::iterator iter;
//数组方式遍历
for(int i=0;i<=s.length()-1;i++){
    cout<<s[i];
}
//迭代器方式
for(iter=s.begin();iter!=s.end();iter++){
    cout<<*iter<<endl;
}

3.字符指针与string互换

string s;
cin>>s;
char buf[128];
strcpy(buf,s.c_str());
//

cout<<"buf:"<<buf<<end;

4.字符串的部分拷贝

string s="xiao jia yu";
char a[20];
s.copy(a,3,3);//从第三个字符开始拷贝3个字符

5.字符串的连接

//第一种方式
string s1="xiao";
string s2="jiayu"
string s3=s1+s2;
//第二种方式
string s4="bang";
s4.append(s3);

6.字符串的查找与替换

string s1="wbm hello wbm 111 wbm 222 wbm 333";
//查找出现wbm的第一个下标
int offindex=s1.find("wbm",0);//从位置0开始
//查找在string中出现wbm所有下标的位置
int offindex=s1.find("wbm",0);//没有找到就会返回-1
while(offindex!=s1.npos){
cout<<"offindex:"<<offindex<<endl;
offindex++;
offindex=s1.find("wbm",offindex);
}
//将所有的wbm换成大写
int offindex=s1.find("wbm",0);
while(offindex!=s1.npos){
    s1.replace(offindex,3,"WBM");
    offindex++;
    offindex=s1.find("wbm",offindex);
}

7、大小写转换

string s1='AAAbbb';
transform(s1.begin(),s1.end(),s1.begin(),toupper);//全部转化为大写
transform(s1.begin(),s1.end(),s1.begin(),tolower);

8.区间的插入与删除

string s1="hello1 hello2 hello3"
int index=s1.find('l',0);
while(index!=s1.npos){
    s1.erase(index,1);
    //index++;删除之后已从下一个开始,当前下标值已换
    index=s1.find("l",index);
}
//全部删除
s1.erase(s1.begin(),s1.end());

完整代码:

#include<string>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
//string的赋值
void f1(){
    string s1="shihao";//静态赋值
    string s2("bbbbbb");
    string s3(s1);//值coPy
    string s4(10,'a');//等价于string s4=("aaaaaaaaaa")
    string s5;
    cin>>s5;//动态输入
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
    cout<<s4<<endl;
    cout<<s5<<endl;
}
//string的遍历
void f2(){
    string s="fanyanfutianxiadiyi";
    string::iterator iter;
    //数组方式
    for(int i=0;i<s.length()-1;i++){
        cout<<s[i]<<"";
    }
    cout<<endl;
    //迭代器方式
    for(iter=s.begin();iter!=s.end();iter++){
        cout<<*iter<<"";

    }
    cout<<endl;
}
//字符指针和string的互换
void f4(){
    string s1="aaabbb";
    cout<<s1.c_str()<<endl;
    //
    char buf[128]={0};
    s1.copy(buf,3,3);//拷贝三个字符,从一个字符开始
    cout<<"buf:"<<buf<<endl;
}
//字符串的连接
void f5(){
    //1:
    string s1="aaa";
    string s2="bbb";
    s1=s1+s2;
    cout<<"s1:"<<s1<<endl;
    //2
    string s3="fanyanfu";
    s3.append(s1);
    cout<<"s3:"<<s3<<endl;
}
//字符串的查找与替换
void f6(){
    string s1="wbm hello wbm 111 wbm 222 wbm 333";
    //查找 从查找位置开始第一个出现的下标
    int index=s1.find("wbm",2);//位置从下标0开始
    cout<<"index:"<<index<<endl<<endl;
    //统计出现wem的所有下标
    int offindex=s1.find("wbm",0);
    while(offindex!=s1.npos){
        cout<<"offindex:"<<offindex<<endl;
        offindex++;
        offindex=s1.find("wbm",offindex);
    }
    //将所有的wbm换成大写
    offindex=s1.find("wbm",0);
    while(offindex!=s1.npos){
        s1.replace(offindex,3,"WBM");
        offindex++;
        offindex=s1.find("wbm",offindex);
    }
    cout<<"替换结果为:"<<s1<<endl;
    //把aaa替换成大写
    string s2="aaa bbb ccc";
    index=s2.find("aaa",0);
    s2.replace(index,3,"AAA");
    cout<<endl<<"s2:"<<s2<<endl;
}
//大小写转换
void f7(){
    string s1="AAAbbb";
    transform(s1.begin(),s1.end(),s1.begin(),toupper);
    cout<<"全部转化为大写:"<<s1<<endl;
    transform(s1.begin(),s1.end(),s1.begin(),tolower);
    cout<<"全部转化为小写:"<<s1<<endl;
}
//区间的插入与删除
void f8(){
    string s1="hello1 hello2 hello1";
    int index=s1.find("l",0);
    while (index!=s1.npos){
        s1.erase(index,1);
        index=s1.find("l",index);
    }
    cout<<"删除后的结果为:"<<s1<<endl;
    s1.erase(s1.begin(),s1.end());
    cout<<"全部删除后的结果:"<<s1<<endl;
}
int main(){
    f4();
    system("pause");
    return 0;

}

猜你喜欢

转载自blog.csdn.net/fyf18845165207/article/details/82729654