> 文章列表 > C++常用字符串string方法

C++常用字符串string方法

C++常用字符串string方法

文章目录

  • 字符串string操作方法
    • 1. 类方法
      • 使用示例
    • 2. 头文件cstring方法
      • 使用示例

字符串string操作方法

1. 类方法

在C++中,引入string.h头文件可以使用C语言中的字符串操作函数。然而,C++提供了一个更加方便的字符串类string,不需要引入string.h头文件,可以直接使用其提供的方法。

后续第二部分会引入<cstring>的方法。<string.h>和<cstring>这两个头文件唯一的区别是,cstring是C++标准库中的头文件,而string.h是C标准库中的头文件。

以下是string类中常用的方法:

  1. length():返回字符串的长度。

  2. size():返回字符串中字符的个数。

  3. at(index):返回字符串中指定位置的字符。

  4. substr(start, length):返回字符串中从指定位置开始的指定长度的子字符串。

  5. append(str):将指定的字符串追加到当前字符串的末尾。

  6. insert(pos, str):在指定位置插入指定的字符串。

  7. erase(pos, length):从指定位置开始删除指定长度的字符。

  8. replace(pos, length, str):用指定的字符串替换从指定位置开始的指定长度的字符。

  9. find(str):在字符串中查找指定的子字符串。find()方法返回的是子字符串在字符串中的位置,如果找不到则返回string::npos

  10. compare(str):比较两个字符串是否相等。

使用示例

#include <iostream>
#include <string>using namespace std;int main() {string str = "Hello World!";// 使用length()方法获取字符串的长度int len = str.length();cout << "Length of str: " << len << endl; // 输出:Length of str: 12// 使用size()方法获取字符串中字符的个数int size = str.size();cout << "Size of str: " << size << endl; // 输出:Size of str: 12// 使用at()方法获取指定位置的字符char ch = str.at(6);cout << "Character at position 6: " << ch << endl; // 输出:Character at position 6: W// 使用substr()方法获取子字符串string subStr = str.substr(6, 5);cout << "Sub-string: " << subStr << endl; // 输出:Sub-string: World// 使用append()方法将字符串追加到末尾str.append(" Goodbye!");cout << str << endl; // 输出:Hello World! Goodbye!// 使用insert()方法在指定位置插入字符串(之前)str.insert(6, "there ");cout << str << endl; // 输出:Hello there World! Goodbye!// 使用erase()方法删除指定位置的字符str.erase(5, 6);cout << str << endl; // 输出:New string: Hello World! Goodbye!// 使用replace()方法替换指定位置的字符str.replace(6, 5, "there");cout << str << endl; // 输出:New string: Hello there! Goodbye!// 使用find()方法查找子字符串size_t pos = str.find("World");if (pos != string::npos) {cout << "Found 'World' at position " << pos << endl; } else {cout << "Unable to find 'World'" << endl;// 输出:Unable to find 'World' }// 使用find()方法查找子字符串pos = str.find("bye");if (pos != string::npos) {cout << "Found 'bye' at position " << pos << endl; // 输出:Found 'bye' at position 17} else {cout << "Unable to find 'bye'" << endl;}// 使用compare()方法比较两个字符串是否相等string str1 = "Hello";string str2 = "hello";int result = str1.compare(str2);if (result == 0) {cout << "Strings are equal" << endl;} else if (result < 0) {cout << "str1 is less than str2" << endl; // √ } else {cout << "str1 is greater than str2" << endl;}return 0;
}

2. 头文件cstring方法

C++中可以使用以下函数来操作字符串:

  1. strlen():返回一个字符串的长度。

  2. strcpy():将一个字符串复制到另一个字符串中。

  3. strcat():将一个字符串追加到另一个字符串的末尾。

  4. strcmp():用于比较两个字符串是否相等。

  5. strstr():在一个字符串中查找另一个字符串。

  6. strtok():用于将一个字符串分割成多个子字符串。

  7. tolower():将字符串中的所有字符转换为小写字母。

  8. toupper():将字符串中的所有字符转换为大写字母。

  9. isalpha():用于判断一个字符是否为字母。

  10. isdigit():用于判断一个字符是否为数字。

这些函数都是C++标准库中提供的字符串操作函数,非常常用。

使用示例

#include <iostream>
#include <cstring>using namespace std;int main()
{char str1[20] = "Hello";char str2[20] = "World";char str3[20];// 使用strcpy将str1复制到str3中strcpy(str3, str1);cout << "str3: " << str3 << endl; // 输出:str3: Hello// 使用strcat将str2追加到str3的末尾strcat(str3, str2);cout << "str3: " << str3 << endl; // 输出:str3: HelloWorld// 使用strcmp比较str1和str2int result = strcmp(str1, str2);if (result < 0){cout << "str1 is less than str2" << endl; // 输出:str1 is less than str2}else if (result > 0){cout << "str1 is greater than str2" << endl;}else{cout << "str1 is equal to str2" << endl;}// 使用strstr在str3中查找"World"char* ptr = strstr(str3, "World");cout << "ptr: " << ptr << endl; // 输出:ptr: World// 使用strtok将str3分割成多个子字符串char* token = strtok(str3, " ");while (token != NULL){cout << token << endl;token = strtok(NULL, " ");}// 输出:// HelloWorldreturn 0;
}