C++字符串操作整理

       标准C++提供了叫做string的新类,该类在多个方面改善了传统的C字符串。虽然字符串类还不是STL中的一部分,但是C++也将其视为一个容器,几乎所有适合容器的算法都能操作string对象。如果要使用string类时,应在程序中包含<string>头文件。

      string类很庞大,含有很多构造函数、成员函数和操作符。我们可以使用他们完成以下任务:

     1.创建字符串对象

     2.从键盘输入字符串

     3.将字符串对象显示在屏幕上

     4.从字符串中找到子串

     5.修改一个字符串对象

     6.比较两个字符串对象

     7.添加字符串对象

     8.访问字符串中的字符

     9.获取字符串的大小

    10.以及众多其他操作


举例:

  一、创建字符串对象

      我们可以使用多种方式创建字符串,如下所示:

     string s1;

     string s2("xyz");

     string s3=s1;

    程序:

   

#include<iostream>
#include<string>

using namespace std;

int main()
{
    //新建字符串对象
    string s1;
    string s2("New");
    string s3("Delhi");

    //给字符串对象赋值
    s1=s2;
    cout<<"S1="<<s1<<endl;

    //用字符串常量赋值
    s1="Standard C++";
    cout<<"Now S1="<<s1<<endl;

    //用另一个对象赋值
    string s4(s1);
    cout<<"S4="<<s4<<endl;

    //通过键盘读入数值赋给对象
    cout<<"ENTER A STRING"<<endl;
    cin>>s4;     //用空格结束
    cout<<"Now S4="<<s4<<endl;

    //连接字符串
    s1=s2+s3;
    cout<<"S1 finally contain:"<<s1<<endl;

    return 0;

}

       二、修改字符串

      通过使用成员函数insert()、replace()、erase()、append()等,我们可以用多种方式修改字符串的内容。

     程序:

    

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s1("12345");
    string s2("abcde");

    cout<<"Original Strings are:"<<endl;
    cout<<"S1:"<<s1<<endl;
    cout<<"S2:"<<s2<<endl;

    //将一个字符串插入另一个字符串
    cout<<"Place S2 inside S1"<<endl;
    s1.insert(4,s2);
    cout<<"Modified S1:"<<s1<<endl;

    //从字符串中删除一个字母
    cout<<"Removing 5 characters from S1"<<endl;
    s1.erase(4,5);
    cout<<"Now S1:"<<s1<<endl;

    //替换字符串中的字母
    cout<<"Replace Middle 3 Characters in S2 with S1"<<endl;
    s2.replace(1,3,s1);
    cout<<"Now S2:"<<s1<<endl
        <<endl;

    return 0;
}

      三、关系操作(字符串对象的关系操作)

       string类定义了一些作用于字符串的操作符。

       程序:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s1("ABC");
    string s2("XYZ");
    string s3=s1+s2;

    if(s1!=s2)
    {
        cout<<"s1 is not equal to s2"<<endl;
    }
    if(s1>s2)
    {
         cout<<"s1 is greater than s2"<<endl;
    }
    else
    {
         cout<<"s2 is greater than s1"<<endl;
    }
    if(s3==s1+s2)
    {
         cout<<"s3 is not equal to s1+s2"<<endl;
    }
    int x=s1.compare(s2);
    if(x==0)
    {
        cout<<"s1==s2"<<endl;
    }
    else if(x>0)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }

    return 0;
}

       四、字符串属性

       类string有很多函数,它们可以用来获取字符串的一些属性,比如大小、长度、容量等。大小或长度指的是字符串中已有字符的个数。而容量是指当前的字符串中能存储的总的元素个数。另一个属性是最大大小,它是指在系统最大限度的支持下,字符串拥有的最大大小。下面程序演示了这些属性的获取和使用。

     程序:

    

#include<iostream>
#include<string>

using namespace std;

void display(string &str)
{
    cout<<"Size="<<str.size()<<endl;
    cout<<"Length"<<str.length()<<endl;
    cout<<"Capacity="<<str.capacity()<<endl;
    cout<<"Maximum="<<str.max_size()<<endl;
    cout<<"Empty="<<(str.empty()?"yes":"no");
    cout<<endl<<endl;
}

int main()
{
    string s1;
    cout<<"Initial status:"<<endl;
    display(s1);

    cout<<"Enter a string (one word)"<<endl;
    cin>>s1;
    cout<<"Status now:"<<endl;
    display(s1);

    s1.resize(15);
    cout<<"Status after resizing:"<<endl;
    display(s1);

    cout<<endl;

    return 0;
}

        五、访问字符串中的字符

       我们可以用多种方式来访问字符串中的字符和字串,string类有以下函数可完成此任务:at()、substr()、find()、find_first_of()、find_last_of()。

       程序:

     

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s("ONE TWO THREE FOUR");

    cout<<"The string contain:"<<endl;
    for(int i=0;i<s.length();i++)
    {
        cout<<s.at(i);
    }
    cout<<endl<<"String is shown again:"<<endl;
    for(int j=0;j<s.length();j++)
    {
        cout<<s[j];
    }

    int x1=s.find("TWO");
    cout<<endl<<endl<<"TWO is found at:"<<x1<<endl;

    int x2=s.find_first_of("T");
    cout<<endl<<"T is found first at:"<<x2<<endl;

    int x3=s.find_last_of("R");
    cout<<endl<<"R is found last at:"<<x3<<endl;

    cout<<endl<<"Retrieve and print substring TWO"<<endl;
    cout<<s.substr(x1,3);

    cout<<endl;

    return 0;
}

   六、比较和交换

     string类比较支持比较和交换函数。compare()函数可以用以比较两个字符串,或者比较两个字符串中的一部分。swap()函数则用以交换两个字符串对象的内容。

    程序:

    

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s1("Road");
    string s2("Read");
    string s3("Red");
    cout<<"s1="<<s1<<endl;
    cout<<"s2="<<s2<<endl;
    cout<<"s3="<<s3<<endl;

    int x=s1.compare(s2);
    if(x==0)
    {
        cout<<"s1==s2"<<endl;
    }
    else if(x>0)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }

    int a=s1.compare(0,2,s2,0,2);
    int b=s2.compare(0,2,s1,0,2);
    int c=s2.compare(0,2,s3,0,2);
    int d=s2.compare(s2.size()-1,1,s3,s3.size()-1,1);

    cout<<"a="<<a<<endl
        <<"b="<<b<<endl
        <<"c="<<c<<endl
        <<"d="<<d<<endl;

    cout<<"Before Swap:"<<endl;
    cout<<"S1="<<s1<<endl
        <<"S2="<<s2<<endl;

    s1.swap(s2);

    cout<<"After Swap:"<<endl;
    cout<<"S1="<<s1<<endl
        <<"S2="<<s2<<endl;
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Tobyuyu/p/4965748.html