c ++ strstr_在C / C ++中使用strstr()的指南

c ++ strstr

In this article, we’ll take a look at using the strstr() function in C / C++.

在本文中,我们将研究在C / C ++中使用strstr()函数。

The strstr() function is very useful if you want to find out whether one string is a sub-string of another. It also gives us the pointer to the first match!

如果要查找一个字符串是否是另一个字符串的子字符串,则strstr()函数非常有用。 它还为我们提供了第一场比赛的指针!

Due to this convenience, strstr() is used somewhat frequently in C, since it is a part of the C standard.

由于这种便利, strstr()在C中经常使用,因为它是C标准的一部分。



C / C ++中strstr()的基本语法 (Basic Syntax of strstr() in C / C++)

This function takes in two input strings search_string and target. Here, we will search the string search_string for the target pattern target.

此函数接受两个输入字符串search_stringtarget 。 在这里,我们将在字符串search_string搜索目标模式target

NOTE: This function ignores the terminal ‘\0’ for both the strings

注意 :此函数将忽略两个字符串的终端“ \ 0”

It returns a char* pointer to the first character of the first match, if found.

返回找到第一个匹配项第一个字符的 char*指针。

If target is not a sub-string of search_string, the returned value is NULL.

如果target不是search_string的子字符串,则返回值为NULL

Also, it is defined in the <string.h> header file, so make sure that you include this header file first!

另外,它是在<string.h>头文件中定义的,因此请确保首先包含此头文件!


#include <string.h>

char* strstr(const char* search_string, const char* target);

Now, this description must be pretty clear to you. Let’s now look at some examples, to exactly see what happens.

现在,此说明对您来说必须非常清楚。 现在让我们看一些示例,以确切地了解发生了什么。

在C / C ++中使用strstr()–一些示例 (Using strstr() in C / C++ – Some Examples)

Let’s take a simple string “Hello from JournalDev”, and search for “JournalDev”. If this function works as expected, we’ll get a pointer to the first match character (“J”).

让我们以一个简单的字符串“ Hello from JournalDev”为例,并搜索“ JournalDev”。 如果此功能按预期工作,我们将获得一个指向第一个匹配字符(“ J”)的指针。

Let’s write our program first.

让我们先编写程序。


#include <stdio.h>
#include <string.h>

int main() {
    char* search_string = "Hello from JournalDev";
    char* target = "JournalDev";

    printf("Search String: %s\n", search_string);
    printf("Target Pattern: %s\n", target);

    char* result = strstr(search_string, target);

    if (result == NULL) {
        printf("Target pattern is not a substring!\n");
    }
    else {
        printf("Target pattern is a substring!\n");
        printf("First character of result = %c\n", *result);
        printf("The complete result string (result) = %s\n", result);
    }

    return 0;
}

Output

输出量


Search String: Hello from JournalDev
Target Pattern: JournalDev
Target pattern is a substring!
First character of result = J
The complete result string (result) = JournalDev

Indeed, we get what we expected! strstr() finds out that “JournalDev” is a substring of “Hello from JournalDev”, and returns a pointer to the first match (“J”).

确实,我们得到了我们所期望的! strstr()发现“ JournalDev”是“ Hello from JournalDev”的子字符串,并返回指向第一个匹配项(“ J”)的指针。

From there, we can simply print the rest of the string, which is a sub-string of the original (“JournalDev”)!

从那里,我们可以简单地打印其余字符串,它是原始字符串(“ JournalDev”)的子字符串!

Now, let’s take another string, which is not a sub-string of the input.

现在,让我们采用另一个字符串,它不是输入的子字符串。


#include <stdio.h>
#include <string.h>

int main() {
    char* search_string = "Hello from JournalDev";
    char* target = "Hi JournalDev";

    printf("Search String: %s\n", search_string);
    printf("Target Pattern: %s\n", target);

    char* result = strstr(search_string, target);

    if (result == NULL) {
        printf("Target pattern is not a substring!\n");
    }
    else {
        printf("Target pattern is a substring!\n");
        printf("First character of result = %c\n", *result);
        printf("The complete result string (result) = %s\n", result);
    }

    return 0;
}

Output

输出量


Search String: Hello from JournalDev
Target Pattern: Hi JournalDev
Target pattern is not a substring!

Since our target pattern is not a sub-string of the input, result will be NULL!

由于我们的目标模式不是输入的子字符串, result将为NULL

Hopefully, these examples make it pretty clear about what strstr() does.

希望这些示例使strstr()作用很清楚。



结论 (Conclusion)

In this article, we learned about using the strstr() function in C / C++. This function makes it easy to check if a string is a sub-string of another string, and also gives a pointer to the first match, if it exists!

在本文中,我们学习了在C / C ++中使用strstr()函数的知识。 这个函数使检查一个字符串是否是另一个字符串的子字符串变得容易,并且还提供了指向第一个匹配项的指针(如果存在)!

参考资料 (References)



翻译自: https://www.journaldev.com/40652/strstr-in-c-plus-plus

c ++ strstr

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值