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;
}
相关推荐
小柯J桑_2 分钟前
C++:STL简介
c++·stl
一颗星星辰3 分钟前
C语言 | 第十章 | 函数 作用域
c语言·开发语言
lxp1997414 分钟前
php函数积累
开发语言·php
科技资讯早知道8 分钟前
java计算机毕设课设—坦克大战游戏
java·开发语言·游戏·毕业设计·课程设计·毕设
白拾19 分钟前
使用Conda管理python环境的指南
开发语言·python·conda
咖啡里的茶i37 分钟前
C++之继承
c++
从0至11 小时前
力扣刷题 | 两数之和
c语言·开发语言
总裁余(余登武)1 小时前
算法竞赛(Python)-万变中的不变“随机算法”
开发语言·python·算法
NormalConfidence_Man1 小时前
C++新特性汇总
开发语言·c++
一个闪现必杀技1 小时前
Python练习2
开发语言·python