strstr函数c语言_C和C ++教程中的strstr()函数以及示例

strstr函数c语言

strstr函数c语言

C and C++ programming languages provide the strstr() function in order to find or match a string in another string. For example, we can search pof string inside the poftut.com and find matches and return the matching index number.

C和C ++编程语言提供了strstr()函数,以便在另一个字符串中查找或匹配一个字符串。 例如,我们可以在poftut.com内搜索pof字符串,找到匹配项并返回匹配的索引号。

strstr()函数语法 (strstr() Function Syntax)

strstr() function has the following syntax where two strings are provided as parameter. strstr() function is case sensitive which means uppercase and lower case matters. For example  pof is will not math with Pofor POF etc.

strstr()函数具有以下语法,其中提供了两个字符串作为参数。 strstr()函数区分大小写,这意味着大写和小写都很重要。 例如pof is不能与PofPOF等数学运算。

const char *strstr(const char *STR1, const char *STR2)
  • `const char *strstr` is the function that will return a pointer or handle as the char data type for the match. If there is no match it will return a null pointer.

    const char * strstr是将返回指针或句柄作为匹配的char数据类型的函数。 如果不匹配,它将返回一个空指针。
  • `const char *STR1` is the string where we will search the STR2. It is constant char pointer simply a string in C and C++.

    const char * STR1是我们将在STR2中搜索的字符串。 它是常量char指针,只是C和C ++中的一个字符串。
  • `const char *STR2` is the term or string which will be searched in STR2.

    const char * STR2是要在STR2中搜索的术语或字符串。

strstr()函数匹配示例 (strstr() Function Matching Example)

We will create a simple example where we will search poftut.com string or char array in the I love the poftut.com string or char array. Before starting in C and C++ string and char array are the same thing just they have a different name but under the hood, they are the same.

我们将创建一个简单的例子,我们将搜索poftut.com字符串或字符数组在I love the poftut.com字符串或字符数组。 在开始使用C和C ++之前,字符串和char数组是相同的东西,只是它们具有不同的名称,但实际上它们是相同的。

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "poftut.com" 
   //and store result into result variable
   result = strstr (str1,"poftut.com");

   //Print result to the standart output
   //This will print characters from first occurence 
   //to the end
   //output is: poftut.com web site
   puts(result);

   return 0;
}

strstr()函数非匹配示例 (strstr() Function NonMatching Example)

In this case, we will make an example where search terms or string do not match or found in the given string. We will search kaleinfo.com inside the string I love poftut.com web site.

在这种情况下,我们将举一个搜索项或字符串与给定字符串不匹配或找不到的示例。 我们将在I love poftut.com web site的字符串中搜索kaleinfo.com

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "kaleinfo.com" 
   //and store result into result variable
   result = strstr (str1,"kaleinfo.com");

   //Create an error because result is null pointer
   puts(result);

   return 0;
}

This example will create an exception because the result is null and when we try to print the result it will create an error or exception.

此示例将创建一个异常,因为结果为null,并且当我们尝试打印结果时,它将创建一个错误或异常。

LEARN MORE  Linux Bash Environment Variables
了解更多Linux Bash环境变量

使用strstr()函数进行字符串替换(Use strstr() Function For String Replacement)

Another useful case for strstr() function is using it for string replacement. We can find the specified string and replace it with the given new string. We will also use the strncpy() function to replace string. We will use I love poftut.com web site and replace the poftut.com with the kaleinfo.com.

strstr()函数的另一个有用案例是将其用于字符串替换。 我们可以找到指定的字符串,并将其替换为给定的新字符串。 我们还将使用strncpy()函数替换字符串。 我们将使用I love poftut.com web site并用poftut.com替换kaleinfo.com

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "poftut.com" 
   //and store result into result variable
   result = strstr (str1,"poftut.com");

   //Replace kaleinfo.com with poftut.com
   strncpy(result,"kaleinfo.com",12);

   //Print result to the standart output
   //This will print characters from first occurence 
   //to the end
   // Output will be: kaleinfo.comeb site
   puts(result);

   return 0;
}

PHP中的strstr()函数 (strstr() Function in PHP)

With the same name and syntax, PHP programming language also provides the strstr() function. This function can be used in PHP version 5.3 and above. In the following example, we will find the user name from the email address and print to the string.

使用相同的名称和语法,PHP编程语言还提供了strstr()函数。 此功能可在PHP 5.3及更高版本中使用。 在下面的示例中,我们将从电子邮件地址中找到用户名并打印到字符串。

<?php
$email_address  = '[email protected]';
$domain_name = strstr($email_address, '@');
echo $domain_name; // prints @poftut.com

$user_name = strstr($email, '@', true); 
echo $user_name; // prints name ismail
?>

翻译自: https://www.poftut.com/strstr-function-in-c-and-cpp-tutorial-with-examples/

strstr函数c语言

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值