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 = "[email protected]";

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

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

int main()
{
    test2();
    cout << "Hello World!" << endl;
    return 0;
}
相关推荐
老胖闲聊4 分钟前
C# 注册表操作类
开发语言·c#
勘察加熊人6 分钟前
Python+Streamlit实现登录页
开发语言·python
理智的煎蛋11 分钟前
keepalived+lvs
java·开发语言·集成测试·可用性测试
☆平常心☆34 分钟前
UE5通过C++实现TcpSocket连接
c++·ue5
Bl_a_ck1 小时前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
编程有点难1 小时前
Python训练打卡Day23
开发语言·python
程序员爱钓鱼1 小时前
跳转语句:break、continue、goto -《Go语言实战指南》
开发语言·后端·golang·go1.19
hardStudy_h1 小时前
C程序的存储空间分配
c语言·开发语言
橙子199110161 小时前
Kotlin 中的 Unit 类型的作用以及 Java 中 Void 的区别
java·开发语言·kotlin
yours_Gabriel1 小时前
【登录认证】JWT令牌
java·开发语言·redis