STL——string

string的存取字符操作:at(index)

string的拷贝操作:copy( ),c_str( )
string的长度:length( ),empty( )
string的赋值:assgin( )
string的字符串连接:append( )
string的比较:compare()

string的子串:substr( )
string的 查找:find( ),rfind()

string的替换:replace( )
 string的插入:insert( )
#include <iostream>
#include <string>
#include <exception>
#include <cstring>

using namespace std;

void StringInit()
{
    string s1;//默认无参
    string s2("helloworld");  //字符串
    string s3(s2);      //拷贝
    string s4(10,'a');    //字符

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}

void StringAt()
{
    string s1("helloworld");

    cout << s1[3] << endl;
    cout << s1.at(5) << endl;

    //cout << s1[500000] << endl;    //越界访问,段错误
    try
    {
        cout << s1.at(10000) << endl;  //越界访问,抛出异常
    }
    catch(exception &e)
    {
        cout << "catch an exception : " << e.what() << endl;
    }
}

void StringStr()
{
    string s("helloworld");
    const char *str;

    str = s.c_str();  //实现字符串复制函数c_str()
    cout << str << endl;
}

void StringCopy()               //拷贝copy()
{
    string s("helloworld");
    char buf[64] = {0};

    s.copy(buf,5,5);   //copy(char *s,int n,int post = 0) n为拷贝个数,post为拷贝起始位置,默认为0
    cout << buf << endl;
}

void StringLength()      //字符串长度length()
{
    string s("helloworld");

    cout << "Length : " << s.length() << endl;   //直接调用length()

    if(s.empty())            //判断是否为空,返回bool类型
    {
        cout << "Null !" << endl;
    }
    else
    {
        cout << "Not Null !" << endl;
    }
}

void StringAssign()          //赋值
{
    string s1("wenwanwan");
    string s2("shixinyu");

    s2 = s1;                          //=
    cout << s2 << endl;

    s2.assign("shixinyu"); //字符串
    cout << s2 << endl;

    s2.assign("wenwanwan",5);  //字符串前5个
    cout << s2 << endl;

    s2.assign(s1);        //string &s型字符串
    cout << s2 << endl;

    s2.assign(5,'a');      //字符
    cout << s2 << endl;

    s2.assign(s1,2,3);         //起始位置2,赋值3个字符
    cout << s2 << endl;

}

void StringAppend()         //连接
{
    string s1("hello");
    string s2("world");
    string s3("test");

    s1 += s2;                       // += string &s
    cout << s1 << endl;
 
    s1 += "love";               //  += 字符串        
    cout << s1 << endl;

    s2.append("love");      //append(字符串)
    cout << s2 << endl;

    s2.append(s3);           //append(string &s)
    cout << s2 << endl;

    s3.append("wenwanwan",3);     //append(字符串,个数)
    cout << s3 << endl;

    s3.append(s2,5,3);          //append(string &s,起始位置,个数)
    cout << s3 << endl;

    s2.append(5,'a');         //字符
    cout << s2 << endl;

}

void StringCompare()            //比较,大于返回1,小于返回-1,等于返回0
{
    string s1("hello");
    string s2("world");

    if(1 == s1.compare(s2))          //compare(string &s)
    {
        cout << "s1 > s2" << endl;
    }
    else if(-1 == s1.compare("wordl"))  //compare(char *s)
    {
        cout << "s1 < s2" << endl;
    }
    else
    {
        cout << "s1 = s2" << endl;
    }
}

void StringSub()              //子字符串
{
    string s1("helloworld");
    cout << s1.substr(0,5) << endl;       //从0位置开始5个字符
}

void StringFind()         //查找,替换
{
    string s1("helloworldhelloworldhelloworld");
    
    int index = s1.find('h',1);   //从1位置查找‘'h'字符
    cout << index << endl;
    index = s1.find("hello",2);     //从2位置查找"hello"字符串
    cout << index << endl;

    index = s1.rfind('h',1);       //反向查找
    cout << index << endl;
    index = s1.rfind("hello",2);
    cout << index << endl;

    s1.replace(5,2,"XXXXXX");    //替换,从5处开始的2个字符替换为"XXXXXX"
    cout << s1 << endl;

    index = s1.find("hello");   //查找并替换
    while(index != -1) //查询不到返回-1
    {
        s1.replace(index,5,"xx");     //从第一次查找"hello"位置处的5个字符替换为"xx"
        index = s1.find("hello",index + strlen("xx"));//继续查找
    }
    cout << s1 << endl; 
}

void StringInsert()     //插入,删除
{
    string s1("helloworld");
    string s2("hi");

    s1.insert(5,"love");    //从5位置处插入字符串
    cout << s1 << endl;
    s1.insert(5,s2);
    cout << s1 << endl;

    s1.erase(5,2);      //删除5位置起的2个字符
    cout << s1 << endl;
}

int main()
{
    //StringInit();
    //StringAt();
    //StringStr();
    //StringCopy();
    //StringLength();
    //StringAssign();
    //StringAppend();
    //StringCompare();
    //StringSub();
    //StringFind();
    StringInsert();

    return 0;
}

string的删除:erase( )

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值