C++常用字符串函数大全(2)

yizhihongxing

C++常用字符串函数大全(2)

本文为C++字符串函数系列文章的第2篇,主要介绍C++标准库中常用的字符串函数,包括:

  • strncpy(): 复制n个字符到目标字符串中。
  • strncat(): 将目标字符串和n个字符的源字符串拼接到一起。
  • strstr(): 在字符串中查找子串。
  • strspn(): 返回目标字符串开头连续包含源字符串字符的数目。
  • strcspn(): 返回目标字符串开头不包含源字符串字符的数目。
  • strpbrk(): 在目标字符串中查找源字符串中任意字符的第一个位置。

1. strncpy()

函数原型:char* strncpy(char* dest, const char* src, size_t count)

该函数可以将源字符串的前count个字符复制到目标字符串中,并返回目标字符串的指针。

示例1:

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

int main() {
    char str1[11] = "hello";
    char str2[6] = "world";

    strncpy(str1, str2, 3);
    cout << str1 << " " << str2;

    return 0;
}

运行结果为:

worlo world

示例2:

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

int main() {
    char str1[11] = "hello";
    char str2[6] = "world";

    strncpy(str1, str2, 6);
    cout << str1 << " " << str2;

    return 0;
}

运行结果为:

world world

2. strncat()

函数原型:char* strncat(char* dest, const char* src, size_t count)

该函数可以将目标字符串和源字符串的前count个字符拼接到一起,并返回目标字符串的指针。

示例1:

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

int main() {
    char str1[11] = "hello";
    char str2[6] = "world";

    strncat(str1, str2, 3);
    cout << str1 << " " << str2;

    return 0;
}

运行结果为:

hellowor world

示例2:

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

int main() {
    char str1[11] = "hello";
    char str2[6] = "world";

    strncat(str2, str1, 6);
    cout << str1 << " " << str2;

    return 0;
}

运行结果为:

hello worldhello

3. strstr()

函数原型:char* strstr(const char* str1, const char* str2)

该函数可以在字符串str1中查找子串str2,并返回第一次找到的位置,如果找不到则返回NULL。

示例1:

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

int main() {
    char str1[9] = "find me!";
    char str2[4] = "me!";

    char* p = strstr(str1, str2);

    if (p)
        cout << "The substring is found at position " << p - str1 << endl;
    else
        cout << "The substring is not found.";

    return 0;
}

运行结果为:

The substring is found at position 5

示例2:

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

int main() {
    char str1[10] = "find me!!";
    char str2[3] = "me";

    char* p = strstr(str1, str2);

    if (p)
        cout << "The substring is found at position " << p - str1 << endl;
    else
        cout << "The substring is not found.";

    return 0;
}

运行结果为:

The substring is found at position 5

4. strspn()

函数原型:size_t strspn(const char* str1, const char* str2)

该函数可以返回字符串str1开头连续包含字符串str2字符的数目。

示例1:

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

int main() {
    char str1[9] = "find me!";
    char str2[5] = "fide";

    size_t len = strspn(str1, str2);

    cout << "The length of the matched substring is " << len;

    return 0;
}

运行结果为:

The length of the matched substring is 2

示例2:

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

int main() {
    char str1[9] = "find me!";
    char str2[5] = "abcd";

    size_t len = strspn(str1, str2);

    cout << "The length of the matched substring is " << len;

    return 0;
}

运行结果为:

The length of the matched substring is 0

5. strcspn()

函数原型:size_t strcspn(const char* str1, const char* str2)

该函数可以返回字符串str1开头不包含字符串str2字符的数目。

示例1:

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

int main() {
    char str1[9] = "find me!";
    char str2[5] = "mn";

    size_t len = strcspn(str1, str2);

    cout << "The length of the unmatched substring is " << len;

    return 0;
}

运行结果为:

The length of the unmatched substring is 4

示例2:

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

int main() {
    char str1[9] = "find me!";
    char str2[5] = "fi";

    size_t len = strcspn(str1, str2);

    cout << "The length of the unmatched substring is " << len;

    return 0;
}

运行结果为:

The length of the unmatched substring is 0

6. strpbrk()

函数原型:char* strpbrk(const char* str1, const char* str2)

该函数可以在字符串str1中查找字符串str2中任意字符的第一个位置,并返回该位置的指针。

示例1:

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

int main() {
    char str1[10] = "findme!";
    char str2[5] = "lm";

    char* p = strpbrk(str1, str2);

    if (p)
        cout << "The first matching character is " << *p << endl;
    else
        cout << "No matching character found.";

    return 0;
}

运行结果为:

The first matching character is m

示例2:

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

int main() {
    char str1[10] = "findme!";
    char str2[5] = "xy";

    char* p = strpbrk(str1, str2);

    if (p)
        cout << "The first matching character is " << *p << endl;
    else
        cout << "No matching character found.";

    return 0;
}

运行结果为:

No matching character found.

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++常用字符串函数大全(2) - Python技术站

(0)
上一篇 2023年6月20日
下一篇 2023年6月20日

相关文章

  • cad备份文件在哪里

    下面我将为您详细讲解如何备份CAD文件。 备份CAD文件的常用方法 在CAD软件内,备份文件有两种常用的方法: 复制文件 打开CAD软件后,选择要备份的文件,右键点击,选择“复制”,再右键点击要复制到的目录,选择“粘贴”。或者使用快捷键Ctrl+C和Ctrl+V进行复制和粘贴。这种方法适用于单个文件的备份。 存储文件 打开CAD软件后,选择“文件” – “另…

    其他 2023年4月16日
    00
  • thinkphp 3.2.3 连接sql server 2014 WAMPSERVER环境包

    thinkphp 3.2.3 连接sql server 2014 WAMPSERVER环境包的完整攻略 本文将为您提供thinkphp 3.2.3连接sql server 2014 WAMPSERVER环境包的完整攻略,包括环境配置、数据库连接、以及两个示例说明。 环境配置 以下是连接sql server 2014 WAMPSERVER环境包的配置步骤: 下…

    other 2023年5月6日
    00
  • jquery和bootstrap

    jQuery和Bootstrap jQuery和Bootstrap 都是非常受欢迎的前端开发库。jQuery是一个JavaScript库,它通过对文档对象模型(Document Object Model,DOM)的操作,使得JavaScript编程更为方便。Bootstrap是由Twitter公司开发的一个开源前端框架,提供了HTML、CSS和JavaScr…

    其他 2023年3月29日
    00
  • Android自定义加载圈的方法

    下面是关于“Android自定义加载圈的方法”的完整攻略,包括两条示例说明。 1.概述 在Android应用中,我们经常需要实现各种各样的加载动画,让用户知道应用正忙着处理任务。其中,比较常见的动画之一就是加载圈。本文将介绍如何通过自定义View来实现一个简单的加载圈效果。 2.实现过程 2.1 创建自定义View 首先,在项目中新建一个自定义View类,名…

    other 2023年6月25日
    00
  • 深入Vue-Router路由嵌套理解

    深入Vue-Router路由嵌套理解攻略 Vue-Router是Vue.js官方的路由管理器,它允许我们在Vue应用中实现页面之间的导航和路由功能。其中一个强大的特性是路由嵌套,它允许我们在一个路由中嵌套另一个路由,从而创建复杂的页面结构和嵌套的组件关系。本攻略将详细讲解Vue-Router路由嵌套的概念和用法。 1. 路由嵌套的基本概念 路由嵌套是指在一个…

    other 2023年7月27日
    00
  • Android实现可滑动的自定义日历控件

    Android实现可滑动的自定义日历控件攻略 1. 概述 在Android中实现可滑动的自定义日历控件可以提供用户友好的日历浏览体验。本攻略将介绍一种实现方法,使用RecyclerView和自定义Adapter来展示日历,并通过手势监听实现滑动功能。 2. 步骤 2.1 创建项目和布局文件 首先,创建一个新的Android项目,并在布局文件中添加一个Recy…

    other 2023年9月6日
    00
  • php实例化对象的实例方法

    下面就来详细讲解一下“PHP实例化对象的实例方法”的完整攻略。 实例化对象 在PHP中,我们可以通过类去创建一个对象,这个过程叫做实例化。代码示例如下: class Animal { // 定义一个属性 public $name; // 定义一个方法 public function showName() { echo "这个动物的名字是:&quot…

    other 2023年6月26日
    00
  • jquery实现界面无刷新加载登陆注册

    实现界面无刷新加载登陆注册的思路可以通过 Ajax 技术来实现。Ajax 是一种通过 JavaScript 在后台与服务器进行数据交换的技术。下面是实现该功能的详细攻略: 1.引入jQuery库文件 使用jQuery时,需要在页面中引入相关的 jQuery 库文件。可以在 jQuery 的官网上下载相关的库文件,也可以通过 CDN 引入,例如: <sc…

    other 2023年6月25日
    00
合作推广
合作推广
分享本页
返回顶部