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;
}
相关推荐
binqian35 分钟前
【异步】js中异步的实现方式 async await /Promise / Generator
开发语言·前端·javascript
林开落L1 小时前
库制作与原理(下)
linux·开发语言·centos·库制作与原理
fengfuyao9851 小时前
基于MATLAB的GUI实现人脸检测、眼睛检测以及LBP直方图显示
开发语言·计算机视觉·matlab
蒋星熠2 小时前
C++零拷贝网络编程实战:从理论到生产环境的性能优化之路
网络·c++·人工智能·深度学习·性能优化·系统架构
CHANG_THE_WORLD2 小时前
# C++ 中的 `string_view` 和 `span`:现代安全视图指南
开发语言·c++
雨落倾城夏未凉2 小时前
9.c++new申请二维数组
c++·后端
Franklin2 小时前
Python界面设计【QT-creator基础编程 - 01】如何让不同分辨率图像自动匹配graphicsView的窗口大小
开发语言·python·qt
雨落倾城夏未凉2 小时前
8.被free回收的内存是立即返还给操作系统吗?为什么?
c++·后端
雨落倾城夏未凉2 小时前
6.new和malloc的区别
c++·后端
郝学胜-神的一滴2 小时前
深入理解QFlags:Qt中的位标志管理工具
开发语言·c++·qt·程序人生