C++自带string类的常用方法

  • 获取字符串长度


#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1 = "hello";

    int length = str1.length();
    printf("调用str.length()函数获取字符串长度:%d\n\n",length );
    return 0;
}
  • 字符串连接


#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    string str4=str1+str2;
    string str5=str3+str2;
    cout<<"字符串str1+str2连接结果:"<<str4<<endl;
    cout<<endl;
    cout<<"字符串str3+str2连接结果:"<<str5<<endl;
    return 0;
}
  • 字符串比较


#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    if (str1 < str3)
        cout << "字符串比较结果:" << "str1<str2" << endl;
    cout << endl;
    return 0;
}
  • 获取字符串的第一个/最后一个字符

         其中begin 得到指向字符串开头的Iterator,end 得到指向字符串结尾的Iterator

         const_iterator 是一个不可以该元素值的迭代器,具体的更多差别可以参考这里:

        https://blog.csdn.net/Krismile_/article/details/89765979


#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    string::const_iterator it = str1.begin();
    cout << *it << endl;


    //end是指向最后一个字符后面的元素,而且不能输出,所以cout << *it << endl;这样输出会报错
    it = str1.end();
    it--;
    cout << *it << endl;
    return 0;
}
  • 倒置串

     注意头文件是#include<algorithm>


#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    reverse(str2.begin(), str2.end());
    cout << "倒置串:" << str2 << endl;
    return 0;
}
  • 字符串转字符数组


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    char *d = new char[20];  //因为下一句那里不是直接赋值,所以指针类型可以不用const char *
    strcpy(d, str3.c_str());  //c_str 取得C风格的const char* 字符串
    cout << "str3:" << c << endl;
    cout << "d:" << d << endl;
    str3 = "hahaha";
    cout << "str3:" << c << endl;
    cout << "d:" << d << endl;
    return 0;
}
  • 查找串


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    string str1 = "hello";
    string str2="my girl!";
    string str3="hello ";

    //find-从指定位置起向后查找,直到串尾
    
    //默认从位置0(即第1个字符)开始查找
    cout << st1.find('e') << endl;
    
    //在st1中,从位置2(l,包括位置2)开始,查找o,返回首次匹配的位置
    cout << st1.find('o', 2) << endl;
    
    //两句均输出1,原因是计算机中-1和4294967295都表示为32个1(二进制)
    cout << (st1.find('c', 0) == -1) << endl;
    cout << (st1.find('c', 0) == 4294967295) << endl;
    
    string st2("aabcbcabcbabcc");
    str1 = "abc";
    
    //6,从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回-1
    cout << st2.find(str1, 2) << endl;
    cout << st2.find("abcdefg", 2, 3) << endl;//6   取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)

    //rfind-从指定位置起向前查找,直到串首
    cout << st1.rfind('a', 7) << endl;

    //find_first_of-在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回-1
    string str6("bcgjhikl");
    string str7("kghlj");
    
    //从str1的第0个字符b开始找,g与str2中的g匹配,停止查找,返回g在str1中的位置2
    cout << str6.find_first_of(str7, 0) << endl;


     //find_last_of-与find_first_of函数相似,只不过查找顺序是从指定位置向前
    string str("abcdecg");
    cout << str.find_last_of("hjlywkcipn", 6) << endl;//从str的位置6(g)开始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5

    //find_first_not_of-在源串中从位置pos开始往后查找,只要在源串遇到一个字符,与目标串中的任意字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,则返回-1
    cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3   从源串str的位置0(a)开始查找,目标串中有a,匹配,..,找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3

    //find_last_not_of-与find_first_not_of相似,只不过查找顺序是从指定位置向前
    cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3
    system("pause");
    cout << str.find_la
}

其他的一些函数:
rbegin 得到指向反向字符串开头的Iterator
rend 得到指向反向字符串结尾的Iterator
max_size 字符串可能的最大大小
capacity 在不重新分配内存的情况下,字符串可能的大小
empty 判断是否为空
operator[] 取第几个元素,相当于数组
data 取得字符串内容地址
operator= 赋值操作符
reserve 预留空间
swap 交换函数
insert 插入字符
append 追加字符
push_back 追加字符
operator+= += 操作符
erase 删除字符串
clear 清空字符容器中所有内容
resize 重新分配空间
assign 和赋值操作符一样
replace 替代
copy 字符串到空间
rfind 反向查找
find_first_of 查找包含子串中的任何字符,返回第一个位置
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
substr 得到字串
compare 比较字符串
operator+ 字符串链接
operator== 判断是否相等
operator!= 判断是否不等于
operator< 判断是否小于
operator>> 从输入流中读入字符串
operator<< 字符串写入输出流
getline 从输入流中读入一行
 

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值