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;
}
相关推荐
devmoon7 小时前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
Evand J7 小时前
TDOA(到达时间差)的GDOP和CRLB计算的MATLAB例程,论文复现,附参考文献。GDOP:几何精度因子&CRLB:克拉美罗下界
开发语言·matlab·tdoa·crlb·gdop
野犬寒鸦7 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
云姜.7 小时前
java抽象类和接口
java·开发语言
xyq20247 小时前
Pandas 安装指南
开发语言
智者知已应修善业8 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法
xixixin_8 小时前
【JavaScript 】从 || 到??:JavaScript 空值处理的最佳实践升级
开发语言·javascript·ecmascript
Trouvaille ~8 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
CSCN新手听安8 小时前
【linux】高级IO,I/O多路转接之poll,接口和原理讲解,poll版本的TCP服务器
linux·运维·服务器·c++·计算机网络·高级io·poll
CSCN新手听安8 小时前
【linux】网络基础(三)TCP服务端网络版本计算器的优化,Json的使用,服务器守护进程化daemon,重谈OSI七层模型
linux·服务器·网络·c++·tcp/ip·json