string 类的用法

访问字符串中的元素

string 字符串也可以像C风格的字符串一样按照下标来访问其中的每一个字符。string 字符串的起始下标仍是从 0 开始。请看下面的代码:

int main(){
    string s = "1234567890";
    for(int i=0,len=s.length(); i<len; i++){
        cout<<s[i]<<" ";
    }
    cout<<endl;
    s[5] = '5';
    cout<<s<<endl;
    return 0;
}

运行结果:
1 2 3 4 5 6 7 8 9 0
1234557890

通过数组的方式实现字符串的单独读取,既然可以通过数组的形式进行读取,那么也可以进行对应位置修改

输入输出

string 类重载了输入输出运算符,可以像对待普通变量那样对待 string 变量,也就是用>>进行输入,用<<进行输出。请看下面的代码:

int main(){
    string s;
    cin>>s;  //输入字符串
    cout<<s<<endl;  //输出字符串
    return 0;
}

输入:
sdasd ssdas
sdasd

原因:>>会默认忽略空格

字符串拼接

有了 string 类,我们可以使用+或+=运算符来直接拼接字符串,非常方便,再也不需要使用C语言中的 strcat()、strcpy()、malloc() 等函数来拼接字符串了,再也不用担心空间不够会溢出了。

用+来拼接字符串时,运算符的两边可以都是 string 字符串,也可以是一个 string 字符串和一个C风格的字符串,还可以是一个 string 字符串和一个字符数组,或者是一个 string 字符串和一个单独的字符。

int main(){
    string s1 = "first ";
    string s2 = "second ";
    char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';
    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;
    
    cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;
    return 0;
}

可以通过加号进行字符串拼接
first second
first third
first fourth
first @


字符串的增删改查

插入字符串

insert()可以在任意位置下增加新的字符串,
用法:

insert (size_t pos, const string& str);

pos 表示要插入的位置,str 表示要插入的字符串,它可以是string字符串,也可以是c风格的

int main(){
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s2.insert(5, "bbb");
    cout<< s2 <<endl;
    return 0;
}

删除

erase()函数可以删除一个string中的一部分
用法:

erase (size_t pos = 0, size_t len = npos);

老样子,前是开始节点 ,后面不太一样,这里是删除的长度 ,如果不指定就默认删除从这个开始到最后的

int main(){
    string s1, s2, s3;
    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    cout<< s3 <<endl;
    return 0;
}

运行结果:
1234567890
12345
1234590

在 pos 参数没有越界的情况下, len 参数也可能会导致要删除的子字符串越界。但实际上这种情况不会发生,erase() 函数会从以下两个值中取出最小的一个作为待删除子字符串的长度:
len 的值;
字符串长度减去 pos 的值。

提取字符串

substr() 为从字符串中提取
用法:

substr(size_t pos =0,size len =npos)const;

pos 为开始位置,len为从开始后的长度

int main(){
    string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    return 0;
}

结果:
first second third
second

字符串查找

find () 函数
用于查找字符串第一次出现的位置;
原型:

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

第一个为用于查找的字符串,他可以是string字符串 ,也可以是c风格字符串
第二个为开始查找的下标

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

运行结果:
Found at index : 6

把second去掉一个字符就是:
NOt found 所以是一整串匹配的

refind()用于查找
只不过是从后往前查找

int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

find_first_of()

这个函数用于查找字符串中具有的共同字符在字符串中首次出现的位置

int main(){
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

Found at index : 3

本例中 s1 和 s2 共同具有的字符是‘s’,该字符在 s1 中首次出现的下标是3,故查找结果返回3。

posted @ 2022-11-04 21:16  E_sheep  阅读(115)  评论(0)    收藏  举报