【C++ 】string——字符串常用函数接口详解

string

在C++中,string是一个表示字符串的数据类型。它是C++标准库中的一部分,可以通过包含 “string” 头文件来使用。

使用string类型可以更方便地处理字符串,它的优点包括:

  1. 自动分配和管理内存:不需要手动分配和释放内存,string类型会根据需要自动处理内存管理,减少了手动管理内存的麻烦。

  2. 方便的字符串操作:string类型提供了丰富的成员函数和操作符,可以方便地进行字符串的拼接、查找、替换、截取等操作。

  3. 支持标准库函数:string类型可以通过使用C库函数进行字符串操作,例如strlenstrcmp等。

1、定义一个字符串

 string s1;    // 初始化一个空字符串
    string s2 = s1;   // 初始化s2,并用s1初始化
    string s3(s2);    // 作用同上
    string s4 = "hello world";   // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中
    string s5("hello world");    // 作用同上
    string s6(6,'a');  // 初始化s6为:aaaaaa
    string s7(s6, 3);  // s7 是从 s6 的下标 3 开始的字符拷贝
    string s8(s6, pos, len);  // s7 是从 s6 的下标 pos 开始的 len 个字符的拷贝

2、读写 string

string输入时遇到空格或回车键才会停止。除了开头的空格外,字符串会读到下一个空格的位置存入string中,空格操作可以用来同时对多个字符串进行初始化。
如果想要读入空格,则需要调用getline函数

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1, s2, s3;    // 初始化一个空字符串
    // 单字符串输入,读入字符串,遇到空格或回车停止
    //aaa
    cin >> s1;  
    // 多字符串的输入,遇到空格代表当前字符串赋值完成,转到下个字符串赋值,回车停止
    // bbb ccc
    cin >> s2 >> s3;  
    string s4 ;    // 初始化一个空字符串
    // abc efd
    getline(cin , s4); 
    
    // 输出字符串 
    cout << s1 << endl;  // 输出aaa
    cout << s2 << endl;  // 输出bbb
    cout << s3 << endl;  // 输出ccc
    cout << s1 << endl;  // 输出abc efd
    return 0;
}

3、字符串连接:

string中重载了+ +=操作符,可以直接使用 字符串+字符串 字符串+=字符串的操作进行字符串的拼接

std::string str1 = "Hello";
std::string str2 = "World";
std::string result = str1 + " " + str2;
std::string str3 = "aaa";
std::string str4 = "bbb";
str3+=str4;

4、字符串访问:

string中重载了[]操作符,可以直接使用 字符串[] 的操作进行字符串的访问

std::string str = "Hello";
char firstChar = str[0];

5、Capacity类接口

  • size:字符串的返回长度
  • length:字符串的返回长度
  • max_size:返回字符串的最大大小
  • resize:调整字符串大小
  • capacity:返回已分配存储的大小
  • reserve:请求更改容量
  • clear:清除字符串
  • empty:测试字符串是否为空
  • shrink_to_fit:缩小以适应
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "abc";    // 初始化一个字符串
    cout << s.empty() << endl;  // s 为空返回 true,否则返回 false
    cout << s.size() << endl;   // 返回 s 中字符个数,不包含空字符
    cout << s.length() << endl;   // 作用同上
    cout << s.capacity() <<endl;	// 返回s的容量大小
    s.reserve(100);	// capacity会被设置为100
    s.clear();	//s会被清空
    return 0;
}

6、Iterators类接口

  • begin:返回一个指向字符串的第一个字符的迭代器

  • end:返回一个指向字符串的最后一个字符之后位置的迭代器

  • rbegin:返回一个指向字符串的最后一个字符之后位置的迭代器

  • rend:返回一个指向字符串的第一个字符的迭代器

  • cbegin:返回开始位置的const迭代器

  • cend:返回一个指向字符串的最后一个字符之后位置的const迭代器

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 使用begin和end遍历字符串
    for (auto it = str.begin(); it != str.end(); ++it) {
        std::cout << *it;
    }
    std::cout << std::endl;
    
    // 使用rbegin和rend逆向遍历字符串
    for (auto it = str.rbegin(); it != str.rend(); ++it) {
        std::cout << *it;
    }
    std::cout << std::endl;
    
    return 0;
}
 

输出结果为:

Hello, World!
!dlroW ,olleH

7、Modifiers类接口

operator+=:字符串追加

append:字符串追加

push_back:尾插一个字符

push_back用于向字符串的末尾添加一个字符。它有以下用法:

  1. 向字符串末尾添加一个字符:
std::string str = "Hello";
str.push_back('!');

在上述示例中,字符串 str 的值将变为 “Hello!”。

  1. 使用循环向字符串末尾逐个添加字符:
std::string str;
for (char ch = 'a'; ch <= 'z'; ch++) {
  str.push_back(ch);
}

在上述示例中,字符串 str 的值将变为 “abcdefghijklmnopqrstuvwxyz”。

push_back 函数可以在现有字符串的末尾添加一个字符。通过循环使用 push_back 函数,可以逐个添加字符来构建字符串。

assign:将一个字符串分配给另一个字符串

assign用于将一个字符串分配给另一个字符串。它有几种不同的用法,例如:

  1. 将一个字符串分配给另一个字符串:
std::string str1 = "Hello";
std::string str2;
str2.assign(str1);
  1. 将一个字符串的一部分分配给另一个字符串:
std::string str1 = "Hello World";
std::string str2;
str2.assign(str1, 6, 5); // 从位置6开始,分配长度为5的子字符串给str2
  1. 将一个C风格的字符串分配给字符串:
const char* cstr = "Hello";
std::string str;
str.assign(cstr); // 将C风格的字符串分配给str
  1. 将一个字符分配给字符串:
char ch = 'A';
std::string str;
str.assign(1, ch); // 将一个字符分配给str

assign 函数可以用于替代使用 = 运算符来赋值字符串。它也可以用于将用其他方式构造的字符串分配给一个已经存在的字符串对象。

insert:插入

insert 用于在字符串中插入指定内容。它有以下用法:

  1. 在指定位置插入一个字符:
std::string str = "Hello";
str.insert(2, "x");

在上述示例中,字符 ‘x’ 将会被插入到字符串 str 的第 2 个位置,字符串的值将变为 “Hexllo”。

  1. 在指定位置插入一个字符串:
std::string str = "Hello";
std::string insertStr = "xxx";
str.insert(2, insertStr);

在上述示例中,字符串 insertStr 将会被插入到字符串 str 的第 2 个位置,字符串的值将变为 “Hexxxllo”。

  1. 在指定位置插入一段字符序列:
std::string str = "Hello";
const char *chars = "xxx";
str.insert(2, chars, 3);

在上述示例中,字符序列 chars 的前 3 个字符将会被插入到字符串 str 的第 2 个位置,字符串的值将变为 “Hexxxllo”。

erase:擦除

字符串中的erase方法可以用来删除字符串中的一部分字符。它有两种用法:

  1. erase(pos, n):从位置pos开始,删除长度为n的字符。例如:
string str = "Hello, world!";
str.erase(7, 6); // 从位置7开始,删除6个字符
cout << str << endl; // 输出:Hello!
  1. erase(iterator):删除指定位置的字符。例如:
string str = "Hello, world!";
str.erase(str.begin() + 7); // 删除位置为7的字符
cout << str << endl; // 输出:Hello orld!

需要注意的是,erase方法会修改原字符串,删除指定的字符。

replace:替换

字符串中的replace方法可以用来替换字符串中的一部分字符。它有三种用法:

  1. replace(pos, n, str):从位置pos开始,替换长度为n的字符为字符串str。例如:
string str = "Hello, world!";
str.replace(7, 5, "everyone"); // 从位置7开始,替换5个字符为"everyone"
cout << str << endl; // 输出:Hello, everyone!
  1. replace(iterator1, iterator2, str):替换从iterator1iterator2之间的字符为字符串str。例如:
string str = "Hello, world!";
str.replace(str.begin() + 7, str.begin() + 12, "everyone"); // 替换从位置7到位置12之间的字符为"everyone"
cout << str << endl; // 输出:Hello, everyone!
  1. replace(iterator1, iterator2, iterator3, iterator4):替换从iterator1iterator2之间的字符为从iterator3iterator4之间的字符。例如:
string str = "Hello, world!";
string replacement = "everyone";
str.replace(str.begin() + 7, str.begin() + 12, replacement.begin(), replacement.end()); // 替换从位置7到位置12之间的字符为"everyone"
cout << str << endl; // 输出:Hello, everyone!

replace方法会修改原字符串,替换指定的字符或字符范围。

swap:交换

swap方法来交换两个字符串的内容。swap方法有两种使用方式:

  1. swap(str):将当前字符串与字符串str进行交换。例如:
string str1 = "Hello";
string str2 = "World";
str1.swap(str2); // 交换str1和str2的内容
cout << str1 << " " << str2 << endl; // 输出:World Hello
  1. swap(index1, index2):将当前字符串中索引为index1和index2的字符进行交换。例如:
string str = "Hello";
str.swap(0, 4); // 将索引为0和4的字符进行交换
cout << str << endl; // 输出:oellH

swap方法会修改原字符串。

pop_back: 尾删

pop_back方法可以用于删除字符串中的最后一个字符。使用pop_back方法后,字符串的长度将减少一个字符。

下面是pop_back方法的使用示例:

string str = "Hello";
str.pop_back(); // 删除最后一个字符
cout << str << endl; // 输出:Hell

8、String operations 类接口

c_str

c_str它用于返回一个指向以null结尾的字符数组(C风格字符串)的指针。

例如,对于一个字符串对象str,可以使用str.c_str()来获取其对应的C风格字符串。

示例代码如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World";

    const char* cstr = str.c_str();

    std::cout << "C-style string: " << cstr << std::endl;

    return 0;
}

上述代码中,str.c_str()返回一个指向字符串 "Hello World" 的C风格字符串的指针,然后我们将这个指针赋值给了一个const char*类型的变量cstr,最后通过std::cout输出这个C风格字符串。

注意,c_str函数返回的指针指向的字符串是常量,不能修改它的内容。

copy

使用copy函数将字符串的一部分复制到一个字符数组中:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    char dest[10];
    
    str.copy(dest, 5, 0); // 将str的前5个字符复制到dest中
    dest[5] = '\0'; // 手动添加字符串结尾符
    
    std::cout << dest << std::endl; // 输出:Hello
    
    return 0;
}
 

substr

使用substr函数返回字符串的子串:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    std::string sub = str.substr(7, 5); // 返回str从索引7开始的长度为5的子串
    
    std::cout << sub << std::endl; // 输出:World
    
    return 0;
}
 

compare

使用compare函数比较两个字符串的大小关系:

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";

    int result = str1.compare(str2); // 比较str1和str2的大小关系

    if (result == 0) {
        std::cout << "str1和str2相等" << std::endl;
    } else if (result < 0) {
        std::cout << "str1小于str2" << std::endl;
    } else {
        std::cout << "str1大于str2" << std::endl;
    }

    return 0;
}
 

find

使用find函数在字符串中查找子串的位置:

#include <iostream>
#include <string>

int main() {
   std::string str = "Hello, World!";
   std::string sub = "World";

   size_t pos = str.find(sub); // 在str中查找sub的位置

   if (pos != std::string::npos) {
       std::cout << "子串'" << sub << "'在位置" << pos << "处找到" << std::endl;
   } else {
       std::cout << "未找到子串'" << sub << "'" << std::endl;
   }

   return 0;
}

find_first_of

在字符串中查找任意一个字符第一次出现的位置。

std::string str = "Hello World";
int index = str.find_first_of("o"); // 找到第一个出现字符'o'的位置
std::cout << index << std::endl;  // 输出6

find_last_of

在字符串中查找任意一个字符最后一次出现的位置。

std::string str = "Hello World";
int index = str.find_last_of("o"); // 找到最后一个出现字符'o'的位置
std::cout << index << std::endl;  // 输出7

find_first_not_of

在字符串中查找第一个不属于给定字符集的字符第一次出现的位置。

std::string str = "Hello World";
int index = str.find_first_not_of("HeloWrd"); // 找到第一个不属于字符集合"HeloWrd"的字符位置
std::cout << index << std::endl;  // 输出5

find_last_not_of

在字符串中查找最后一个不属于给定字符集的字符最后一次出现的位置。

std::string str = "Hello World";
int index = str.find_last_not_of("HeloWrd"); // 找到最后一个不属于字符集合"HeloWrd"的字符位置
std::cout << index << std::endl;  // 输出10
 

※ 如果文章对你有帮助的话,可以点赞收藏!!谢谢支持

  • 32
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一岁就可帅-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值