C++之string

C++之string


#include <iostream>

using namespace std;

/*
 string();//创建一个空的字符串
 string(const char* s);//使用字符串s初始化
 string(const string& str);//使用一个string对象初始化另外一个string对象
 string(int n,char c);//使用n个字符c初始化
 */

void test1()
{
    string s1;//默认构造
    const char *str = "hello woreld!";
    string s2(str);
    cout << "s2 = "<<s2<<endl;

    string s3(s2);
    cout << "s3 = "<<s3<<endl;

    string s4(5,'b');
    cout << "s4 = "<<s4<<endl;
}

int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

/*
string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符赋值给当前的字符串
string& assign(const char *s);//把字符串s赋给当前的字符串
string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s);//把字符串s给当前字符串
string& assign(int n, char c);//用n个字符c赋给当前字符串
 */

void test1()
{
    string s1 = "hello woreld!";
    cout << "s1 = "<<s1<<endl;

    string s2(s1);
    cout << "s2 = "<<s2<<endl;

    string s3 ;
    s3 = 'b';
    cout << "s3 = "<<s3<<endl;

    string s4;
    s4.assign("hello woreld!");
    cout << "s4 = "<<s4<<endl;

    string s5;
    s5.assign("hello woreld!",5);
    cout << "s5 = "<<s5<<endl;

    string s6;
    s6.assign(s5);
    cout << "s6 = "<<s6<<endl;

    string s7;
    s7.assign(5,'b');
    cout << "s7 = "<<s7<<endl;
}

int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

/*
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& operator+=(const string& str);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=(const string& str)
string& append(const string &s,int pos,int n); //字符s中从pos开始的n个字符连接到字符串结尾
 */

void test1()
{
    string s1 = "I";
    cout << "s1 = "<<s1<<endl;

    s1 += " LOVE GAME";
    cout << "s1 = "<<s1<<endl;

    string s2 = ":";
    s1 += s2;
    cout << "s1 = "<<s1<<endl;

   // s1.append(" LOL DNF");
   // s1.append(" LOL DNF",4);
    string s3 = " LOL DNF";
    //s1.append(s3);
    s1.append(s3,0,4);
    cout << "s1 = "<<s1<<endl;



}

int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}



#include <iostream>
#include<string>
using namespace std;


//查找
void test1()
{
    string str1 = "abcdefgde";

    int pos = str1.find("de");

    if(pos == -1)
    {
        cout<<"no find"<<endl;
    }
    else
    {
        cout<<"finded , pos = "<<pos<<endl;
    }

    //find和rfind的区别
    //find:从左往右查找;rfind:从右往左查找
    int pos2 = str1.rfind("de");
    if(pos2 == -1)
    {
        cout<<"no find"<<endl;
    }
    else
    {
        cout<<"finded , pos = "<<pos2<<endl;
    }
}

int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}
void test2()
{
    string str1 = "abcdefgde";
    //将bc替换成2222
    str1.replace(1,3,"2222");
    cout<<str1<<endl;
}
int main()
{
    test2();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;


//查找
void test1()
{
    string str1 = "hello";

    string str2 = "xello";

    if(str1.compare(str2) == 0)
    {
        cout<<"str1 = str2"<<endl;
    }
    else if(str1.compare(str2) > 0)
    {
        cout<<"str1 > str2"<<endl;
    }
    else
    {
        cout<<"str1 < str2"<<endl;
    }
}


int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

//string字符存取
void test1()
{
    string str1 = "hello";
    //cout<<"str1 = "<<str1<<endl;

    //通过[]访问单个字符
    for(unsigned int i = 0;i < str1.size();i++)
    {
        cout<<str1[i]<<" ";
    }
    cout<<endl;
    //通过at方式访问单个字符
    for(unsigned int i = 0;i<str1.size();i++)
    {
        cout<<str1.at(i)<<" ";
    }
    cout<<endl;

    //通过[]修改单个字符
    str1[0] = 'x';
    cout<<"str1 = "<<str1<<endl;
    //通过at方式修改单个字符
    str1.at(1) = 'x';
    cout<<"str1 = "<<str1<<endl;
}


int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

//string字符插入和删除
void test1()
{
    string str1 = "hello";
    cout<<"str1 = "<<str1<<endl;

    //插入
    str1.insert(1,"222");
    cout<<"str1 = "<<str1<<endl;

    //删除
    str1.erase(1,3);
    cout<<"str1 = "<<str1<<endl;
}


int main()
{
    test1();
    cout << "Hello World!" << endl;
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

//string字符中求子串
void test1()
{
    string str1 = "hello";
    cout<<"str1 = "<<str1<<endl;


    string subStr = str1.substr(2,2);
    cout<<"subStr = "<<subStr<<endl;
}

//使用的例子  获取邮箱的用户名
void test2()
{
    string str2 = "zhangsan@sina.com";

    int pos = str2.find("@");

    string subStr = str2.substr(0,pos);
    cout<<"subStr = "<<subStr<<endl;
}

int main()
{
    test2();
    cout << "Hello World!" << endl;
    return 0;
}
相关推荐
蜀黍@猿22 分钟前
【C++ 基础】从C到C++有哪些变化
c++
Am心若依旧40924 分钟前
[c++11(二)]Lambda表达式和Function包装器及bind函数
开发语言·c++
明月看潮生26 分钟前
青少年编程与数学 02-004 Go语言Web编程 20课题、单元测试
开发语言·青少年编程·单元测试·编程与数学·goweb
zh路西法34 分钟前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(一):从电梯出发的状态模式State Pattern
c++·决策树·状态模式
大G哥36 分钟前
java提高正则处理效率
java·开发语言
VBA63371 小时前
VBA技术资料MF243:利用第三方软件复制PDF数据到EXCEL
开发语言
轩辰~1 小时前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
小_太_阳1 小时前
Scala_【1】概述
开发语言·后端·scala·intellij-idea
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
lxyzcm1 小时前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23