c++ string

1 sting 基本概念

复制代码
string 基本概念
本质:string是c++风格的字符串,而string 本质上是一个类

string 和char* 的区别:
char * 是一个指针
string 是一个类,类内部封装了char*,管理这个字符串,是一个char* 数组

2 构造函数原型

复制代码
string();//创建一个空的字符串 例如: string str;
string(const char* s);//使用单个字符串初始化
string(const string& str);//使用一个string对象初始化
string(const char* str, int n);//使用n个字符串初始化
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

3. 赋值操作

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// sring 赋值操作
/*
- string& operator=(const char* s);  // char*类型字符串赋值给当前的字符串
- string& operator=(const string& str);  // 把字符串str赋值给当前字符串
- string& operator=(char c);  // 字符赋值给当前字符串
- string& assign(const char* s);  // 把字符串s赋值给当前字符串
- string& assign(const char* s, int n);  // 把字符串s的前n个字符赋值给当前字符串
- string& assign(const string& str);  // 把str这个字符串赋值给当前字符串
- string& assing(int n, char c);  // 用n个字符c赋值给当前字符串
*/

void test01() {
    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(str4);
    cout << "str6 = " << str6 << endl;

    string str7;
    str7.assign(5, 'x');
    cout << "str7 = " << str7 << endl;
}


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

4 字符串拼接

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 字符串拼接

void test01() {
    string str1 = "hello";
    str1 += "world"; // 字符串拼接
    cout << str1 << endl;   
    str1 += '!'; // 字符 拼接
    cout << str1 << endl;

    string str2 = "hello";
    str1 += str2; // 字符串拼接
    cout << str1 << endl;

    string str3 = "我 ";
    str3.append("爱"); // 字符串拼接
    cout << str3 << endl;

    str3.append("game adcde", 4); // 取前4个字符
    cout << str3 << endl;

    str3.append(str2, 0, 2); // str2 从下标0开始 拼接2个字符
    cout << str3 << endl;
}


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

5 string 查找和替换

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// string 查找和替换

void test01() {
    // 查找
    string str = "abcdefghijklde";
    int pos = str.find("de");
    if (pos != -1)
    {
        cout << "find pos: " << pos << endl;
    }
    else{
        cout << "not find" << endl;
    }
    // rfind 从右边开始查找
    pos = str.rfind("de");
    cout << "rfind pos: " << pos << endl;
}

// 替换
void test02() {
    string str = "abcdefghijklde";
    str.replace(1, 3, "11111"); // 从第1个位置开始,替换3个字符,替换成11111
    cout << str << endl;
}

int main(int argc, char const *argv[]) {
    test02();
    return 0;
}

6. 字符串比较

复制代码
比较方式:
字符串比较是按照字符的ASCII码进行比较的
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 字符串比较

void test01() {
    string s1 = "xello";
    string s2 = "zello";
    if(s1.compare(s2) == 0){
        cout << "s1 == s2" << endl;
    }
    else if(s1.compare(s2) > 0){
        cout << "s1 > s2" << endl;
    }
    else{
        cout << "s1 < s2" << endl;
    }
}


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

7 string 字符存取

string 中单个字符存取方式有两种

复制代码
char& operator[](int n); // 通过[ ]方式取字符
char& at(int n); // 通过at 方法取字符
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// string 字符存取
void test01() {
    string s1 = "hello";
    // 通过[]访问字符串中单个字符
    for (int i = 0; i < s1.size(); i++)
    {
       cout << s1[i] ;
    }
    cout << endl;
    // 通过at访问字符串中单个字符
    for (int  i = 0; i < s1.size(); i++)
    {
       cout << s1.at(i);
    }
    cout << endl;
    
    // 修改字符串
    s1[0] = 'x';
    cout << s1 << endl;
    s1.at(1) = 'x';
    cout << s1 << endl;
    
}


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

8 string 字符串插入和删除

复制代码
函数原型
string& insert(int pos, const string& str); //在pos位置插入str
string& insert(int pos, const char* s); //在pos位置插入s
string& insert(int pos, int n, char c); //在pos位置插入n个字符c
string& erase(int pos, int n = npos); //删除从pos开始的n个字符
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 字符串插入和删除

// 函数原型
// string& insert(int pos, const string& str); //在pos位置插入str
// string& insert(int pos, const char* s); //在pos位置插入s
// string& insert(int pos, int n, char c); //在pos位置插入n个字符c
// string& erase(int pos, int n = npos); //删除从pos开始的n个字符


void test01() {
    string str = "hello";
    // 插入
    str.insert(1, "111");
    cout << str << endl;
    // 删除
    str.erase(1, 3);
    cout << str << endl;
}


int main(int argc, char const *argv[]) {
    test01();
    return 0;
}

9. string 子串

从字符串中获取想要的子串

函数原型:

复制代码
string substr (size_t pos = 0, size_t len = npos) const; //返回一个字符串,包含从pos开始的len个字符
cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 从字符串中获取想要的子串
// 函数原型:
// string substr (size_t pos = 0, size_t len = npos) const; //返回一个字符串,包含从pos开始的len个字符


void test01() {
    string str = "hello world";
    string subStr = str.substr(0, 5);
    cout << "subStr = " << subStr << endl;
}

// 实用操作
void test02() {
    string str = "[email protected]";
    // 从字符串中获取邮件名称
    int pos = str.find("@");
    cout << "pos = " << pos << endl;
    string name = str.substr(0, pos);
    cout << "name = " << name << endl;
}


int main(int argc, char const *argv[]) {
    test02();
    return 0;
}
相关推荐
舔甜歌姬的EGUMI LEGACY3 分钟前
【算法day28】解数独——编写一个程序,通过填充空格来解决数独问题
算法
welkin16 分钟前
KMP 个人理解
前端·算法
橘猫云计算机设计21 分钟前
基于JavaWeb的二手图书交易系统(源码+lw+部署文档+讲解),源码可白嫖!
java·开发语言·前端·毕业设计·php
半桔22 分钟前
红黑树剖析
c语言·开发语言·数据结构·c++·后端·算法
江烽渔火30 分钟前
C++ 多态
开发语言·c++
eason_fan31 分钟前
前端面试手撕代码(字节)
前端·算法·面试
今天_也很困38 分钟前
牛客2025年愚人节比赛
c++·算法
嘤国大力士39 分钟前
C++11&QT复习 (十)
java·c++·qt
Joe_Wang540 分钟前
[图论]拓扑排序
数据结构·c++·算法·leetcode·图论·拓扑排序
m0_490240671 小时前
软件自动化测试(1):python+selenium自动化测试环境搭建
开发语言·python·selenium