C++常用字符串string方法

字符串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和str2
    int 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, " ");
    }
    // 输出:
    // HelloWorld

    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

如果皮卡会coding

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

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

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

打赏作者

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

抵扣说明:

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

余额充值