C++之字符串操作学习笔记

string基本概念

  • 本质:string是C++风格的字符串,string本质上是一个类
    string和char *的区别:
  • char *是一个指针
  • string是一个类,类内部封装了char *,是一个char *容器
    特点:
  1. string类内部封装了很多成员方法:find查找,delete删除、replace删除、insert插入
  2. string管理char *所分配的内存,不用担心复制越界和取值越界的问题,由类内部进行负责
string构造函数

构造函数原型:

  • string();创建一个空的字符串,例如string str1;
  • string(const char *s);使用字符串s初始化
  • string(const string& str);使用一个string对象初始化另一个string对象
  • string(int n, char c)使用n个字符c初始化
#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string s1; // 默认构造
    const char *str = "hello,world";
    string s2(str);
    cout << s2 << endl; // 输出hello,world
}

int main()
{
    test1();
    return 0;
}
string赋值操作
#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string str1 = "hello,world";

    string str2;
    str2.assign("hello,c++"); // 使用string类的成员函数进行赋值

    string str3;
    str3.assign("hello,c++", 5); // 把字符串中的前5个字符赋值给str3
    cout << str3 << endl;        // 输出hello

    string str4;
    str4.assign(str3); // 拷贝赋值,利用另外一个字符串,给str3赋值

    string str5;
    str5.assign(10, 'w'); // 输出10个w
    cout << str5 << endl;
}

int main()
{
    test1();
    return 0;
}
字符串拼接

功能描述:实现在字符串末尾拼接字符串

#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string str1 = "hello";
    str1 += ",world"; // 追加一个字符串
    str1 += '.';      // 追加一个字符
    cout << str1 << endl;

    string str2 = "test";
    str2 += str1;
    cout << str2 << endl;

    // 追加方式2:采用string类的成员函数
    string str3 = "test2";
    str3.append(" ,"); // 追加字符串或字符

    str3.append("hello,world", 4); // 追加字符串hello,world的前4个字符
    cout << str3 << endl;

    // 参数2:从哪个位置开始截取,参数3:截取字符个数
    str3.append(str2, 0, 3); // 从字符串str2的第0个位置(起始位置)截取3个字符,追加到str3

    cout << str3 << endl;
}

int main()
{
    test1();
    return 0;
}
字符串查找和替换

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串
    示例1:字符串查找find()rfind()
  • find查找是从左往右,rfind是从右往左
  • find找到字符串后返回查找到的第一个字符位置(起始位置为0),找不到返回-1
  • replace在替换时,要指定从那个位置起,多少个连续的字符,替换为str
#include <iostream>
#include <string>
using namespace std;

// 字符串查找
void test1()
{
    string str1 = "abcdef";
    int pos = str1.find("de"); // 默认从0号位置查找 字符串第一次出现的位置
    if (pos == -1)
    {
        cout << "未找到字符串" << endl;
    }
    cout << pos << endl; // 输出结果:3(起始位置为0)。如果没有找到,输出:-1

    // rfind 和 find的区别
    // rfind 从右往左开始查找。find从左往右开始查找
    pos = str1.rfind("a");
    cout << pos << endl;  // 输出结果0
}

int main()
{
    test1();
    return 0;
}

示例2:字符串替换replace()

#include <iostream>
#include <string>
using namespace std;

// 字符串查找
void test1()
{
    string str1 = "abcdef";

    // 参数1:起始位置,参数2:从pos起始位置开始把连续的3个字符,替换为字符串str
    str1.replace(1, 3, "1");
    cout << str1 << endl; // 输出结果:a1ef 。把bcd这三个字符,替换成了字符串str
}

int main()
{
    test1();
    return 0;
}
字符串比较compare()
  • 功能描述:字符串之间的比较,从起始位置开始比
  • 比较方式:按字节的ASCII码进行对比
    • =,输出0
    • >,输出1
    • <,输出-1
      总结:字符串对比主要用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大。
#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string str1 = "bcd";
    string str2 = "abcde";
    int result = str1.compare(str2);

    cout << result << endl; // 输出1
}

int main()
{
    test1();
    return 0;
}
字符串存取

功能描述:存取单个字符

#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string str1 = "abcd";
    cout << str1.size() << endl; // 输出4,不包括字符串的结束符
    // 访问方式1:通过[]访问单个字符
    for (int i = 0; i < str1.size(); i++)
    {
        cout << str1[i] << " ";
    }
    cout << endl;

    // 访问方式2:通过at访问
    for (int i = 0; i < str1.size(); i++)
    {
        cout << str1.at(i) << " ";
    }
    cout << endl;

    // 修改单个字符
    str1[0] = 'x'; // str1.at(0) = 'x';

    cout << str1 << endl;
}

int main()
{
    test1();
    return 0;
}
字符串插入和删除
  • insert()插入字符串
  • erase()删除从pos开始的m个字符
#include <iostream>
#include <string>
using namespace std;

void test1()
{
    // 插入
    string str1 = "abcd";
    str1.insert(1, "123"); // 在第一个字符之后,插入字符串
    cout << str1 << endl;  // 输出:a123bcd

    // 删除
    str1.erase(1, 3);     // 从第一个位置开始,删除3个字符
    cout << str1 << endl; // 输出:abcd
}

int main()
{
    test1();
    return 0;
}

总结:插入和删除的起始下标都是从0开始。

字符串截取substr()
  • 功能描述:从字符串中获取想要的子串
#include <iostream>
#include <string>
using namespace std;

void test1()
{
    string str1 = "abcdefg";
    string subStr = str1.substr(1, 3); // 从1号位置,截取3个连续的字符
    cout << subStr << endl;            // 输出:bcd
}

// 实用操作
void test2()
{
    string email = "hello@sina.com";
    // 从邮件地址中获取用户信息
    int pos = email.find("@");
    cout << pos << endl;
    string username = email.substr(0, pos); // 输出:hello
    cout << username << endl;
}

int main()
{
    test1();
    test2();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值