C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)

简介: C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)

一、字符串构造

// 字符串的构造
void test01() {
    //默认构造
    string s1;
    //使用char*字符串构造
    const char *str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
    //使用string对象初始化
    string s3(s2);
    cout << "s3=" << s3 << endl;
    //使用n个字符初始化
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
};

二、字符串赋值

/**
 * 赋值操作
 * 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个字符赋给当前字符串
 */
void test02() {
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;
    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;
    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;
    string str4;
    str4.assign("hello C++");
    cout << "str4 = " << str4 << endl;
    string str5;
    str5.assign("hello C++", 5);
    cout << "str5 = " << str5 << endl;
    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;
    string str7;
    str7.assign(10, 'e');
    cout << "str7 = " << str7 << endl;
};

三、字符串拼接

//string字符串拼接
/**
 *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 test03() {
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << "str1 = " << str1 << endl;
    str1 += ":";
    cout << "str1 = " << str1 << endl;
    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;
 
    string str3 = "I";
    str3.assign(" love ");
    cout << "str3 = " << str3 << endl;
 
    str3.append("game abcde", 5);
    cout << "str3 = " << str3 << endl;
 
    // str3.append(str2);
    str3.append(str2, 0, 3);
    cout << "str3 = " << str3 << endl;
 
};

四、字符串查找

//字符串查找
void test04() {
    string str = "abcdefgde";
    // 从左往右查
    int pos = str.find("de");
    cout << "pos = " << pos << endl;
    //从右往左查
    pos = str.rfind("de");
    cout << "rfind pos = " << pos << endl;
};

五、字符串替换

//字符串替换
void test05() {
    string str1 = "abcdefg";
    //从1位置起2个字符,替换为"1231231"
    str1.replace(1, 2, "1231231");
    // str1=a1231231defg
    cout << "str1=" << str1 << endl;
};

六、字符串比较

//字符串比较
// = 返回 0
// > 返回 1
//< 返回 -1
void test06() {
    string str1 = "aello";
    string str2 = "hello";
    if (str1.compare(str2) == 0) {
        cout << "str1等于str2" << endl;
    } else if (str1.compare(str2) > 0) {
        cout << "str1大于str2" << endl;
    } else {
        cout << "str1小于str2" << endl;
    }
};

七、字符串存取

//string 字符存取
void test07() {
    string str = "hello";
    // cout << "str=" << str << endl;
    //1、通过[]访问单个字符
    for (int i = 0; i < str.size(); ++i) {
        cout << str[i] << " ";
    }
    cout << endl;
    //2、通过at方法访问单个字符
    for (int i = 0; i < str.size(); ++i) {
        cout << str.at(i) << " ";
    }
    cout << endl;
    //修改单个字符
    str[0] = 'x';
    str.at(1) = 'x';
    // xxllo
    cout << "str=" << str << endl;
}

八、字符串插入删除

/*
 * string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
 */
//字符串的插入和删除
void test08() {
    string str = "hello";
//     插入
    str.insert(1, "111");
    // h111ello
    cout << str << endl;
    //hello
    str.erase(1, 3);
    cout << str << endl;
}

九、子串

//string子串
void test09() {
    string str = "abcdef";
    string subStr = str.substr(1, 3);
    // subStr=bcd
    cout << "subStr=" << subStr << endl;
 
    string email = "zhangsan@sina.com";
    //从邮件地址中获取用户名信息
    int pos = email.find("@");
    cout<<pos<<endl;
    string userName = email.substr(0, pos);
    cout << userName << endl;
}

十、测试

#include <iostream>
#include <string>
 
using namespace std;
 
// 字符串的构造
void test01() {
    //默认构造
    string s1;
    //使用char*字符串构造
    const char *str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
    //使用string对象初始化
    string s3(s2);
    cout << "s3=" << s3 << endl;
    //使用n个字符初始化
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
};
 
/**
 * 赋值操作
 * 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个字符赋给当前字符串
 */
void test02() {
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;
    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;
    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;
    string str4;
    str4.assign("hello C++");
    cout << "str4 = " << str4 << endl;
    string str5;
    str5.assign("hello C++", 5);
    cout << "str5 = " << str5 << endl;
    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;
    string str7;
    str7.assign(10, 'e');
    cout << "str7 = " << str7 << endl;
};
//string字符串拼接
/**
 *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 test03() {
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << "str1 = " << str1 << endl;
    str1 += ":";
    cout << "str1 = " << str1 << endl;
    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;
 
    string str3 = "I";
    str3.assign(" love ");
    cout << "str3 = " << str3 << endl;
 
    str3.append("game abcde", 5);
    cout << "str3 = " << str3 << endl;
 
    // str3.append(str2);
    str3.append(str2, 0, 3);
    cout << "str3 = " << str3 << endl;
 
};
 
//字符串查找
void test04() {
    string str = "abcdefgde";
    // 从左往右查
    int pos = str.find("de");
    cout << "pos = " << pos << endl;
    //从右往左查
    pos = str.rfind("de");
    cout << "rfind pos = " << pos << endl;
};
 
//字符串替换
void test05() {
    string str1 = "abcdefg";
    //从1位置起2个字符,替换为"1231231"
    str1.replace(1, 2, "1231231");
    // str1=a1231231defg
    cout << "str1=" << str1 << endl;
};
 
//字符串比较
// = 返回 0
// > 返回 1
//< 返回 -1
void test06() {
    string str1 = "aello";
    string str2 = "hello";
    if (str1.compare(str2) == 0) {
        cout << "str1等于str2" << endl;
    } else if (str1.compare(str2) > 0) {
        cout << "str1大于str2" << endl;
    } else {
        cout << "str1小于str2" << endl;
    }
};
 
    //string 字符存取
    void test07() {
        string str = "hello";
        // cout << "str=" << str << endl;
        //1、通过[]访问单个字符
        for (int i = 0; i < str.size(); ++i) {
            cout << str[i] << " ";
        }
        cout << endl;
        //2、通过at方法访问单个字符
        for (int i = 0; i < str.size(); ++i) {
            cout << str.at(i) << " ";
        }
        cout << endl;
        //修改单个字符
        str[0] = 'x';
        str.at(1) = 'x';
        // xxllo
        cout << "str=" << str << endl;
    }
/*
 * string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
 */
//字符串的插入和删除
void test08() {
    string str = "hello";
//     插入
    str.insert(1, "111");
    // h111ello
    cout << str << endl;
    //hello
    str.erase(1, 3);
    cout << str << endl;
}
 
//string子串
void test09() {
    string str = "abcdef";
    string subStr = str.substr(1, 3);
    // subStr=bcd
    cout << "subStr=" << subStr << endl;
 
    string email = "zhangsan@sina.com";
    //从邮件地址中获取用户名信息
    int pos = email.find("@");
    cout<<pos<<endl;
    string userName = email.substr(0, pos);
    cout << userName << endl;
}
 
int main() {
    // test01();
    // test02();
    // test03();
    // test04();
    // test05();
    // test06();
    // test07();
    // test08();
    test09();
    system("pause");
    return 0;
}
 
subStr=bcd
8
zhangsan

相关文章
|
20天前
|
存储 人工智能 Python
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
13 0
|
3月前
|
索引 Python
String(字符串)
String(字符串)。
54 3
|
4月前
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
88 2
|
3月前
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
57 0
|
4月前
|
NoSQL Redis
Redis 字符串(String)
10月更文挑战第16天
63 4
|
4月前
|
canal 安全 索引
(StringBuffer和StringBuilder)以及回文串,字符串经典习题
(StringBuffer和StringBuilder)以及回文串,字符串经典习题
52 5
|
4月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
100 2
|
29天前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
66 19
|
29天前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
46 13
|
29天前
|
编译器 数据安全/隐私保护 C++
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
48 5