C++ String 类

在 C++ 中,string 类用于将一系列字符表示为该类的一个对象。

我们可以通过在文件中包含 <string> 头文件来访问各种 string 类函数。

#include <string>

字符串类函数

常用的 string 类函数有。

函数 描述
find() 在字符串中查找子字符串。
rfind() 查找子字符串在字符串中最后一次出现的位置。
append() 追加到字符串。
insert() 插入到字符串中。
erase() 从字符串中删除字符。
replace() 替换字符串中的一部分。
compare() 比较两个字符串。

示例 1:在给定字符串中查找子字符串

find() 函数搜索字符串,查找指定子字符串的第一次出现,并返回第一次匹配的第一个字符的位置

rfind() 函数搜索字符串,查找指定子字符串的最后一次出现,并返回最后一次匹配的第一个字符的位置

例如,

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

int main() {

    string str = "Hello world, wonderful world!";
    cout << "String: " << str << endl;
    
// find the first occurrence size_t first_occurrence = str.find("world"); // find the last occurrence size_t last_occurrence = str.rfind("world");
if (first_occurrence != string::npos) { cout << "First occurrence: 'world' found at position: " << first_occurrence << endl; cout << "Last occurrence: 'world' found at position: " << last_occurrence << endl; } else { cout << "'world' not found" << endl; } return 0; }

输出

String: Hello world, wonderful world!
First occurrence: 'world' found at position: 6
Last occurrence: 'world' found at position: 23

if (first_occurrence != string::npos) 检查变量 first_occurrence 是否包含子字符串 world 在字符串中被找到的有效位置。

  • 如果 world字符串中被找到,first_occurrence 将被设置为其第一次出现的位置。
  • 如果 world未被找到,它将被设置为一个特殊常量值 string::npos,表示该子字符串在字符串中未被找到。

注意:这两个函数都区分大小写。因此,如果我们的字符串是 Hello World, wonderful world!,那么 find() 函数将返回23作为该子字符串的第一次出现。


示例 2:追加到字符串

我们可以使用 append() 函数将一个字符串追加到现有字符串。例如,

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

int main() {

    string str = "Hello world,";
    cout << "Before: " << str << endl;

//append the string str.append(" Have a good day!");
cout << "After: " << str << endl; return 0; }

输出

Before: Hello world,
After: Hello world, Have a good day!

示例 3:在给定位置插入字符串

我们可以使用 insert() 函数在任何给定位置插入一个字符串。例如,

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

int main() {

    string str = "Hello world, world!";
    cout << "Before: " << str << endl;
    
// insert "beautiful" at the 13th index str.insert(13, " beautiful");
cout << "After: " << str << endl; return 0; }

输出

Before: Hello world, world!
After: Hello world,  beautifulworld!

在这里,我们在字符串的第 13 个索引处插入了子字符串 beautiful。请注意,这并不会在添加的字符串末尾添加空格。


示例 4:从给定位置删除 N 个字符

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

int main() {

    string str = "Hello world, beautiful world!";
    cout << "Before: " << str << endl;
    
// erase five characters starting from the seventh index str.erase(7, 5);
cout << "After: " << str << endl; return 0; }

输出

Before: Hello world, beautiful world!
After: Hello w beautiful world!

在这里,我们使用 erase() 函数从第七个索引开始删除了五个字符。


示例 5:替换字符串中的 N 个字符

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

int main() {

    string str = "Hello world, beautiful world!";
    cout << "Before: " << str << endl;
    
// replace two characters with "Earth" // starting from the seventh index str.replace(6, 2, "Earth");
cout << "After: " << str << endl; return 0; }

输出

Before: Hello world, beautiful world!
After: Hello Earthrld, beautiful world!

在这里,我们用 Earth 替换了两个字符(wo)。我们可以使用 replace() 函数用任意长度的另一个字符串替换任意长度的字符串。


示例 6:按字母顺序比较字符串

为了获得字符串之间的字典序关系,我们使用 compare() 函数。

C++ string 类中的 compare() 函数返回一个整数,该整数指示所比较字符串之间的字典序关系。它返回

  • 0 - 如果被比较的字符串相等。
  • < 0 - 如果调用字符串按字典序小于被比较的字符串。
  • > 0 - 如果调用字符串按字典序大于被比较的字符串。

例如,

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

int main() {

    string str1 = "Hello world";
    string str2 = "Hello world";
    string str3 = "Hello";
    string str4 = "Hello world, What's Up?";
    
    cout << "String 1: " << str1 << endl;
    cout << "String 2: " << str2 << endl;
    cout << "String 3: " << str3 << endl;
    cout << "String 4: " << str4 << endl;
    
// compare the strings cout <<"Comparing String 1 and String 2: " << str1.compare(str2) << " (Equal)" <<endl; cout <<"Comparing String 1 and String 3: " << str1.compare(str3) << " (String 1 is Longer)" << endl; cout <<"Comparing String 1 and String 4: " << str1.compare(str4) <<" (String 1 is Smaller)" << endl;
return 0; }

输出

String 1: Hello world
String 2: Hello world
String 3: Hello
String 4: Hello world, What's Up? 
Comparing String 1 and String 2: 0 (Equal)
Comparing String 1 and String 3: 6 (String 1 is Longer)
Comparing String 1 and String 4: -12 (String 1 is Smaller)

这里,

  • str1.compare(str2) 返回0,因为 str1str2 相等。
  • str1.compare(str3) 返回一个正数,因为 str1 按字典序大于 str3(它包含更多字符)。
  • str1.compare(str4) 返回一个负数,因为 str1 按字典序小于 str4
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战