十、C++_String类

一、string类简述

        在C语言里,字符串是用字符数组来表示的,而对于应用层而言,会经常使用到字符串,而继续使用字符数组,就使得效率非常低,所以在C++标准库里,通过类string重新自定义了字符串。

头文件:#include <string>

·string 直接支持字符串连接

·string 直接支持字符串的大小比较

·string 直接支持子串查找和提取

·string 直接支持字符串的插入和替换

·string 同时具备字符串数组的灵活性,可以通过[ ]重载操作符来访问每个字符

二、常用构造方法

    //1
    string s1;
    cout<<s1<<endl;//s1 = ""
    cout<<s1.size()<<endl;//0 字符串的长度,不包括‘\0’
    cout<<s1.length()<<endl;//0 字符串的长度,不包括‘\0’
    //2
    string s2("hello");//用字符串常量构造string对象 string(const char *)
    cout<<s2<<endl;//s2 ="hello"
    //3
    const char* p="world";
    string s3(p);
    cout<<s3<<endl;//s3="world"
    //4
    string s4(4,'A');
    cout<<s4<<endl;//s4="AAAA"
    //5
    string s5("12345",1,3);
    cout<<s5<<endl;//s5="234",即“12345”的下标1开始,长度为3的子串

注意:string 类没有接收一个整型参数或者一个字符型参数的构造函数。下面两种错误:

string s1('K');

string s2(123);

三、对 string 对象的赋值

1、可以使用 char* 类型的变量、常量,以及char类型的变量、常量对string 对象进行赋值,如:

    string s1;

    s1 = "hello";//char *
    cout<<s1<<endl;//s1 = "hello"

    s1 = 'A';//char s1="A"
    cout<<s1<<endl;//s1 = "A"

2、assign 成员函数

    string s1("abcdef"),s2;
    s2.assign(s1);   //等价 s2=s1;
    cout<<s2<<endl;//abcdef

    s2.assign(s1,1,3);//从下标1开始,取3个字符;
    cout<<s2<<endl;//bcd

    s2.assign(3,'A');//s2="AAA";
    cout<<s2<<endl;//AAA

四、字符串的拼接 

1、使用运算符+拼接两个字符串

    string s1="hello";

    string s2="world";

    string s3=s1+s2;//直接字符串相加

    cout <<s3<<endl;//"helloworld"

2、使用append成员函数拼接字符串,append函数返回对象自身的引用

   string s1("123"),s2("abc");

   string s3;

   s3=s1.append(s2);//将s2追加到s1后面 s1=s1+s2,并且会返回对象自身的引用

   cout<<s1<<endl;

   cout<<s3<<endl;

   s1.append(s2,1,2);//将s2下标为1开始追加2个字符串

   cout<<s1<<endl;//123abcbc

   s1.append(3,'K');//在字符串末尾追加3个‘K’

   cout<<s1<<endl;//123abcbcKKK

   s1.append("ABCDE",2,3);//从字符串下标为2开始追加3个字符

   cout<<s1<<endl;//123abcbcKKKCDE

 五、string 对象比较大小

1、可以使用 <、<=、==、!=、>=、> 运算符比较string对象

    string s1 ="hello";

    string s2 ="world";

    bool ret;

    ret=s1>s2;//'h'的ASCII值小于'w',所以返回false

    if(ret)

        cout<<"s1>s2"<<endl;

    else

        cout<<"s1<s2"<<endl;//输出s1<s2

2、使用 compare 成员函数,compare成员函数有以下返回值:

·小于0表示当前的字符串小;

·等于0表示两个字符串相等;

·大于0表示另一个字符串小。

    string s1 ="hello";

    string s2 ="hello,world";

    int n = s1.compare(s2);

        cout<<n<<endl;//-6

    n=s1.compare(1,2,s2,0,3);//比较s1的子串(1,2) el 和s2的子串(0,3) hel

        cout<<n<<endl;//-1

    n=s1.compare(0,2,s2);//比较s1的子串(0,2) hel 和s2

        cout<<n<<endl;//-9

    n=s1.compare("Hello");

        cout<<n<<endl;//1

    n=s1.compare(1,2,"Hello");//比较s1的子串(1,2)和"Hello"

        cout<<n<<endl;//1

    n=s1.compare(1,2,"Hello",1,2);//比较s1的子串(1,2) el 和"Hello"的子串(1,2) el

        cout<<n<<endl;//0

 六、求 string 对象的子串

substr 成员函数可以用于从字符串中提取子字符串,原型如下:

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

pos:表示要提取的子字符串的起始位置。字符串的位置从0开始计数,如果pos超过了字符串的长度,则函数将抛出 std::out_of_range 异常。

len:表示要提取的子字符串的长度。如果len超过了从pos开始到字符串末尾的长度,那么将只提取从pos到字符串末尾的部分。npos是一个特殊的值,代表直到字符串的末尾。

   //1.提取整个字符串

   string str = "hello,world!";

   string sub = str.substr(0,str.length());

   cout<<sub<<endl;//输出"hello,world!";

   //2.提取特定长度的子字符串

   sub = str.substr(6,5);//提取从下标为6开始的5个字符

   cout<<sub<<endl;//输出"world"

   //3.提取直到字符串末尾的子字符串

   sub = str.substr(6);//提取从下标为6开始到字符串末尾的所有字符

   cout<<sub<<endl;//输出"world!"

 七、Swap成员函数

       swap通常用于两个对象之间的值交换。对于std::string类,swap函数允许你交换两个字符串的内容,而不是创建新的字符串对象,这样可以提高效率,尤其是在处理大型字符串时。声明如下:

void swap(std::string& other);

参数other是一个引用,指向另一个std::string对象。调用swap函数会交换当前对象和other对象中的字符串内容。这个操作是就地进行的(in-place),也就是说,它不会创建新的字符串,也不会销毁任何字符串,它只是简单地交换两个字符串内部的数据。

   //1.交换两个字符串

   string str1 = "hello";

   string str2 = "world";

   swap(str1,str2);//hello和world进行交换

   cout<<str1<<" "<<str2<<endl;//输出"world hello"

   //2.使用成员函数交换字符串

   str1.swap(str2);//在交换一次

   cout<<str1<<" "<<str2<<endl;//输出"hello world"

八、查找子串和字符

1.find成员函数是std::string类提供的一个功能,用于在字符串中从左到右查找特定的子字符串或字符

std::string类的 find 函数的基本语法如下:

size_t find(const std::string& str,size_t pos=0) const;

size_t find(char c,size_t pos=0) const;

 ·str:表示要查找的子字符串。

·c:表示要查找的单个字符。

·pos:表示开始查找的位置。如果从位置pos开始没有找到匹配项,函数将返回std::string::npos,这是一个特殊的值,表示未找到匹配项。

   //1.查找子字符串
    string str = "hello,world!";
    int pos = str.find("world");//pos=6,world是从下标为6开始的
    if(pos != std::string::npos){
        cout<<"found 'world' at position: "<<pos<<endl;
    }else{
        cout<<"not found"<<endl;
    }
    //2.查找单个字符
    pos = str.find('o');//pos=4,从左到右找到的第一个'o'的下标为4
    if(pos != std::string::npos){
        cout<<"found 'o' at position: "<<pos<<endl;
    }else{
        cout<<"not found"<<endl;
    }
    //3.从特定位置开始查找
    pos = str.find('o',5);//pos=7,从下标为5的位置开始向后查找'o'
    if(pos != std::string::npos){
        cout<<"found 'o' at position: "<<pos<<endl;
    }else{
        cout<<"not found"<<endl;
    }

注意:

  • find函数是区分大小写的。例如,str.find("world")在"Hello, World!"中会返回std::string::npos
  • 如果pos参数超出了字符串的长度,find函数将抛出std::out_of_range异常。
  • find函数的返回值是子字符串或字符在字符串中第一次出现的起始位置的索引。如果没有找到匹配项,则返回std::string::npos

2、rfind成员函数是std::string类提供的一个功能,用于在字符串中从右到左查找特定的子字符串或字符。std::string类的rfind函数的基本语法如下:

size_t rfind(const std::string& str,size_t pos = npos) const noexcept;

size_t rfind(char c,size_t pos=npos) const noexcept;

 ·str :表示要查找的子字符串

·c:表示要查找的单个字符

·pos:表示开始查找的位置。这个位置是从右到左的索引,如果pos超出了字符串的长度,函数将从字符串的末尾开始查找。

    //1.从右到左查找子字符串
    string str = "hello,world!";
    int pos = str.rfind("world");//6 
    if(pos != string::npos){
        cout<<"found 'world' at position: "<<pos<<endl;
    }else{
        cout<<"not found"<<endl;
    }
    //2.从右到左查找单个字符
    pos = str.rfind('e',6);//1 ,从下标6开始向左查找
    if(pos != string::npos){
        cout<<"found 'e' at position: "<<pos<<endl;
    }else{
        cout<<"not found"<<endl;
    }

九、string类replace成员函数

        在C++中,replace成员函数是std::string类提供的一个功能,它允许你修改字符串的内容

1、替换指定范围内的内容:从位置pos开始,删除n1个字符,然后用str中的字符替换它们。

void replace(size_t pos,size_t n1,const std::string& str);
string s="hello,world!";
s.replace(6,5,"there");//从下标为6开始的5个字符替换为there
cout<<s<<endl;//输出"hello,there!"

2、替换指定范围内的内容为字符:从位置pos开始,删除n1个字符,然后用n2c字符替换它们。

void replace(size_t pos,size_t n1,size_t n2,char c);
string s="hello,world!";
s.replace(6,5,3,'*');//从下标6开始删除5个字符然后使用3个‘*’替换
cout<<s<<endl;//输出"hello,***!"

 3、替换指定范围内的内容为另一个字符串的一部分:

void replace(size_t pos1,size_t pos2,const std::string& str,size_t n1,size_t n2);
string s="hello,world!";
s.replace(1,4,"123456",2,5);//用"123456"的子串(2,5)替换s的子串(1,4)
cout<<s<<endl;//输出"h3456,world!"

十、erase成员函数

erase成员函数可以删除string对象中的子串,返回值为对象自身的引用

string str="hello,world!";
//1.删除下标位置1开始的3个字符的子串
str.erase(1,3);//删除子串(1,3) ell
cout<<str<<endl;//输出ho,world!
//2.删除从下标5开始的所有字符
str.erase(5);//删除下标5及其后面的所有字符
cout<<str<<endl;//输出ho,wo

 十一、insert成员函数插入字符串

insert成员函数可以在string对象中插入另一个字符串,返回值为对象自身的引用

//1.在指定位置插入单个字符
string str="hello";
str.insert(4,"w");//在下标为4的位置插入一个字符'w'
cout<<str<<endl;//hellwo/
//2.在指定位置插入另一个字符串
string str1="world";
str.insert(6,str1);//在下标为6的位置插入字符串str1
cout<<str<<endl;//hellwoworld
//3.一次插入多个相同的字符
str.insert(2,3,'o');//在下标为2的位置插入3个'o'
cout<<str<<endl;//heooollwoworld
//4.将字符串的子串插入到指定位置
str1.insert(5,"there",2,5);//将"there"的子串(2,5)插入到str1下标为5的位置
cout<<str1<<endl;//worldere

 

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值