逐梦C++之五:string类型

1.String对象的定义及其初始化

C++提供string类型来代替C语言中以NULL结尾的char类型数组。string类型支持长度可变的字符串,并可以满足对字符串的一般应用。
例子:
//1.几种初始化string对象的方式

    string s1;//默认构造函数,s1为空串
    string s2(s1);//将s2初始化为s1的一个副本
    string s3("value");//将s3初始化为一个字符串字面值副本
    string s4(n,'c');//将s4初始化为字符'c'的n个副本

2.转化为C风格的字符串

在程序中我们经常遇到需要C风格字符串的情况。例如,我们打开文件时必须要求,文件名必须要求一个C风格类型的字符串,这时我们使用函数c_str()<该函数将返回指向一个const的null结尾的char类型数组的指针>将string类型的字符串转换C风格类型的字符串;
例子:

//2.转换为C风格的字符串:c_str()
    string filename="infile.dat";
    ifstream infile;
    infile.open(filename.c_str());

3.字符串长度length()

函数length()将返回一个字符串的长度。
例子:

//3.返回字符串的长度:length()
    string s5("Hello World!");
    cout<<"s5长度="<<s5.length()<<endl;

4.读写string

操作符<<可用来输出string类型的字符串。
例子:

//4.读写string
    string s6("Hello World!");
    string s7(s6);
    string s8(10,'a');
    cout<<"s6="<<s6<<'\n'
            <<"s7="<<s7<<'\n'
            <<"s8="<<s8<<'\n';

操作符>>用来输入string类型的字符串,其默认动作是忽略前导空格,读取内容直到另一个空格为止或文件结束;
函数getline(a,b)常用来读取一整行到string类型的变量中去,其中,a是输入流(比如cin或者文件对象),b是string类型的变量;
例子:

//4.1getline().读取一整行数到string类型的变量中
    string buff;
    ifstream infile0;
    ofstream outfile0;
    cout<<"请输入读取数据的文件名:"<<endl;
    cin>>buff;
    infile0.open(buff.c_str());
    cout<<"请输入输出数据的文件名:"<<endl;
    cin>>buff;
    outfile0.open(buff.c_str());
    while(getline(infile0,buff))
        outfile0<<buff<<"\n\n";
    infile0.close();
    outfile0.close();

5.字符串的连接

操作符+和+=可用来进行字符串的连接;

+:(左右位置可以互换)
string字符串+string字符串
string字符串+C风格字符串
string字符串+char字符 
+=:(左右位置不能互换)
string+=string字符串
string+=C风格字符串
string+=char字符

6.函数erase(a,b):

从字符串中删除一个字符串,第一个参数是需要删除的子串开始的索引位置,第二个参数是需要删除的子串的长度。如果不指明第二个参数,则从第一个参数给出的索引位置到string末尾的所有字符全部删除。如果两个参数都不指明,则删除string字符串的所有字符,最终得到一个空串;
例子:

//erase():从字符串中删除一个子串
    string s9("Happy new year to u!");
    cout<<"s9="<<s9<<endl;
    s9.erase(7,3);
    cout<<"s9.erase(7,3)="<<s9<<endl;
    s9.erase();
    cout<<"s9.erase()="<<s9<<endl;

运行结果:
这里写图片描述

7.函数insert(a,b)

在某个string字符串的指定位置插入另外一个字符串,第一个参数给出插入位置的索引,第二个参数给出要插入的字符串
例子:

//insert(a,b):在某一string字符串中插入指定的字符串b
    string s10("Happy year to u!");
    cout<<"插入前:s10="<<s10<<endl;
    s10.insert(6,"new ");
    cout<<"插入后:s10="<<s10<<endl;

8.函数replace(a,b,c):

用一个指定的字符串来代替子串,a是子串开始的索引位置,b是子串的长度,c是指定的字符串
例子:

    //replace(a,b,c):用指定的字符串来替代string字符串中的一个子串
    string s11("Happy old year to u!");
    cout<<"替换之前:s11="<<s11<<endl;
    s11.replace(6,3,"new");
    cout<<"替换之后:s11="<<s11<<endl;

9.swap(b):

调换两个字符串。
例子:

    //swap(b):调换两个字符串
    string s12("Hello!");
    string s13("Good Morning!");
    cout<<"调换之前:s12="<<s12<<endl
            <<"调换之前:s13="<<s13<<endl;
    s12.swap(s13);
    cout<<"调换之后:s12="<<s12<<endl
            <<"调换之后:s13="<<s13<<endl;

10.操作符[]可以用来访问string字符串指定索引位置的字符;

例子:

    //[]:操作符可以用来访问string字符串指定位置的字符
    string s14("Happy new year to u!");
    cout<<"s14=";
    for (int i=0;i<s14.length();i++)
        cout<<s14[i];
    cout<<endl;

11.函数substr(a,b):

函数sunstr(a,b)用来提取子串,a是子串首位字符的索引,b是子串的长度;
例子:

//substr(a,b):用来提取子串
    string s15("Happy new year to u!");
    string s16;
    s16=s15.substr(6,9);
    cout<<"提取的子串s16="<<s16<<endl;

12.find(a,b)/rfind(a,b):

函数find(a,b/rfind(a,b))用来在一个字符串中查找子串:
find(a,b):在string字符串中查找子串a,并且查找的开始索引位置大于等于b,如果找到,则返回查找到的子串在string字符串中的首位字符的索引值,若没找到则返回无穷大;
rfind(a,b):rfind()和find()类似,只是查找时索引值要小于等于b
例子:

    //find(a,b)/rfind(a,b):查找子串
    string s17("Happy new year to u!");
    string s18("new");
    string s19("Hello");
    int f;
    f=s17.find(s18,9);
    if(f<s17.length())
        cout<<"找到该子串:该子串首位字符在string字符中的索引位置为:"<<f<<endl;
    else
        cout<<"查无此子串"<<endl;
    f=s17.rfind(s18,9);
    if(f<s17.length())
        cout<<"找到该子串:该子串首位字符在string字符中的索引位置为:"<<f<<endl;
    else
        cout<<"查无此子串"<<endl;

13.find_first_of(a)/find_first_not_of(b):

find_first_of(a):查找string字符串和a字符串都具有的第一个字符的索引,若找到,返回该字符串在string字符串中的索引,否则,返回无穷大;
find_first_not_of(b):查找在string字符串中但不在b字符串中首位字符的索引,若找到,则返回该字符在string字符串中的索引,否则返回无穷大
//find_first_of(a)/find_first_not_of(b):查找都具有的第一个字符的索引/查找string字符串中具有,但b字符串中不具有的首位字符索引
例子:
    string s19("Abby");
    string s20("bx");
    cout<<"s19.find_first_of(s20)"<<s19.find_first_of(s20)<<endl;
    cout<<"s19.find_first_not_of(s20)"<<s19.find_first_not_of(s20)<<endl;

14.字符串的比较

操作符==,!=,<=,<,>,>=可以用来进行字符串的比较。
例子:

    //==,!=,<,<=,>,>=:字符串的比较
    string s21("panorama");
    string s22("panda");

    if(s21==s22)
        cout<<"s21==s22"<<endl;
    if(s21!=s22)
        cout<<"s21!=s22"<<endl;
    if(s21<s22)
        cout<<"s21<s22"<<endl;
    if(s21<=s22)
        cout<<"s21<=s22"<<endl;
    if(s21>s22)
        cout<<"s21>s22"<<endl;
    if(s21>=s22)
        cout<<"s21>=s22"<<endl;

15.综合例子:

//==================================================
//程序描述:浅谈C++string类型
//Date:2016年1月20日   by  Ye_wolf
//备注:
//==================================================

//========================【头文件部分】=============================
#include <iostream>
#include <string>
#include <fstream>
//#include <iomanip>
using namespace std;

//=========================【主函数】============================
int main ()
{
    int n=10;

    //1.几种初始化string对象的方式
    string s1;//默认构造函数
    string s2(s1);//将s2初始化为s1的一个副本
    string s3("value");//将s3初始化为一个字符串字面值副本
    string s4(n,'c');//将s4初始化为字符'c'的n个副本

    //2.转换为C风格的字符串:c_str()
    string filename="infile.dat";
    ifstream infile;
    infile.open(filename.c_str());
    infile.close();

    //3.返回字符串的长度:length()
    string s5("Hello World!");
    cout<<"s5长度="<<s5.length()<<endl;

    //4.读写string
    string s6("Hello World!");
    string s7(s6);
    string s8(10,'a');
    cout<<"s6="<<s6<<'\n'
            <<"s7="<<s7<<'\n'
            <<"s8="<<s8<<'\n';

    //4.1getline().读取一整行数到string类型的变量中
    /*string buff;
    ifstream infile0;
    ofstream outfile0;
    cout<<"请输入读取数据的文件名:"<<endl;
    cin>>buff;
    infile0.open(buff.c_str());
    cout<<"请输入输出数据的文件名:"<<endl;
    cin>>buff;
    outfile0.open(buff.c_str());
    while(getline(infile0,buff))
        outfile0<<buff<<"\n\n";
    infile0.close();
    outfile0.close();*/

    //erase(a,b):从字符串中删除一个子串
    string s9("Happy new year to u!");
    cout<<"s9="<<s9<<endl;
    s9.erase(7,3);
    cout<<"s9.erase(7,3)="<<s9<<endl;
    s9.erase();
    cout<<"s9.erase()="<<s9<<endl;

    //insert(a,b):在某一string字符串中插入指定的字符串b
    string s10("Happy year to u!");
    cout<<"插入前:s10="<<s10<<endl;
    s10.insert(6,"new ");
    cout<<"插入后:s10="<<s10<<endl;

    //replace(a,b,c):用指定的字符串来替代string字符串中的一个子串
    string s11("Happy old year to u!");
    cout<<"替换之前:s11="<<s11<<endl;
    s11.replace(6,3,"new");
    cout<<"替换之后:s11="<<s11<<endl;

    //swap(b):调换两个字符串
    string s12("Hello!");
    string s13("Good Morning!");
    cout<<"调换之前:s12="<<s12<<endl
            <<"调换之前:s13="<<s13<<endl;
    s12.swap(s13);
    cout<<"调换之后:s12="<<s12<<endl
            <<"调换之后:s13="<<s13<<endl;

    //[]:操作符可以用来访问string字符串指定位置的字符
    string s14("Happy new year to u!");
    cout<<"s14=";
    for (int i=0;i<s14.length();i++)
        cout<<s14[i];
    cout<<endl;

    //substr(a,b):用来提取子串
    string s15("Happy new year to u!");
    string s16;
    s16=s15.substr(6,9);
    cout<<"提取的子串s16="<<s16<<endl;

    //find(a,b)/rfind(a,b):查找子串
    string s17("Happy new year to u!");
    string s18("new");
    int f;
    f=s17.find(s18,9);
    if(f<s17.length())
        cout<<"找到该子串:该子串首位字符在string字符中的索引位置为:"<<f<<endl;
    else
        cout<<"查无此子串"<<endl;
    f=s17.rfind(s18,9);
    if(f<s17.length())
        cout<<"找到该子串:该子串首位字符在string字符中的索引位置为:"<<f<<endl;
    else
        cout<<"查无此子串"<<endl;

    //find_first_of(a)/find_first_not_of(b):查找都具有的第一个字符的索引/查找string字符串中具有,但b字符串中不具有的首位字符索引
    string s19("Abby");
    string s20("bx");
    cout<<"s19.find_first_of(s20)"<<s19.find_first_of(s20)<<endl;
    cout<<"s19.find_first_not_of(s20)"<<s19.find_first_not_of(s20)<<endl;

    //==,!=,<,<=,>,>=:字符串的比较
    string s21("panorama");
    string s22("panda");

    if(s21==s22)
        cout<<"s21==s22"<<endl;
    if(s21!=s22)
        cout<<"s21!=s22"<<endl;
    if(s21<s22)
        cout<<"s21<s22"<<endl;
    if(s21<=s22)
        cout<<"s21<=s22"<<endl;
    if(s21>s22)
        cout<<"s21>s22"<<endl;
    if(s21>=s22)
        cout<<"s21>=s22"<<endl;
    return 0;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值