首页  >  文章  >  后端开发  >  C# 包含()

C# 包含()

WBOY
WBOY原创
2024-09-03 15:17:50487浏览

用于检查给定字符串中是否存在字符串的方法在 C# 中称为 Contains() 方法。如果给定字符串中存在该字符串,则返回 true,如果给定字符串中不存在该字符串,则返回 true给定字符串,返回 false,因此返回类型为 System.引发 Boolean 和 ArgumentNullException。如果必须检查其中是否存在字符串的输入字符串为空,并且检查字符串是否存在给定字符串是基于区分大小写。

语法:

public bool Contains(String string)

其中 string 是给定的字符串,必须检查该字符串是否在其中出现。

Bool 是 Contains 方法返回的布尔值,如果给定字符串中存在该字符串,则设置为 true;如果给定字符串中不存在该字符串,则设置为 false。

C# Contains() 方法的工作原理

  • 每当需要查找一个字符串在另一个字符串中出现的情况时,我们都会使用 Contains 方法来检查给定字符串以查找其中另一个字符串的出现情况。
  • True 是 contains() 方法返回的值(如果该字符串存在于给定字符串中)。
  • 如果给定字符串中不存在该字符串,则 contains() 方法返回的值是 False。
  • 如果必须检查其中字符串是否出现的输入字符串为 null,并且检查字符串中是否存在给定字符串是基于区分大小写,则会引发 ArgumentNullException。

实现 C# Contains() 的示例

以下是 C# Contains() 的示例:

示例#1

C# 程序演示 Contains() 方法来检查给定字符串以查找该字符串在给定字符串中的出现情况。

代码:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string which will be checked to find the occurrence of another string within it
string st1 = "C Sharp";
//another string variable is used to store the string which will be checked for in the given string
string st2 = " ";
//another string variable is used to store the string which will be checked for in the given string
string st3 = "Welcome";
//Contains() method is used to check the string for the occurrence of another string within it
Console.WriteLine("If there is occurrence of the string in the given string:{0} ", st1.Contains(st2));
Console.WriteLine("If there is occurrence of the string in the given string:{0} ",st1.Contains(st3));
}
}

输出:

C# 包含()

说明:在上面的程序中,定义了一个名为program的类。然后调用 main 方法,其中使用字符串变量来存储字符串,检查该字符串以查找其中是否存在另一个字符串。然后使用另一个字符串变量来存储在第一个字符串中搜索其出现的字符串。然后,再次使用另一个字符串变量来存储在第一个字符串中搜索其出现的字符串。然后使用 Contains() 方法检查字符串中是否存在另一个字符串。如果第一个字符串中出现了另一个字符串,则返回 True;如果其中没有出现另一个字符串,则返回 false。

示例#2

C# 程序演示 Contains() 方法来检查给定字符串以查找该字符串在给定字符串中的出现情况。

代码:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string which will be checked to find the occurrence of another string within it
string st1 = "Welcome to C Sharp";
//another string variable is used to store the string which will be checked for in the given string
string st2 = "#";
//another string variable is used to store the string which will be checked for in the given string
string st3 = "Welcome";
//Contains() method is used to check the string for the occurrence of another string within it
Console.WriteLine("If there is occurrence of the string in the given string:{0} ", st1.Contains(st2));
Console.WriteLine("If there is occurrence of the string in the given string:{0} ",st1.Contains(st3));
}
}

输出:

C# 包含()

说明:在上面的程序中,定义了一个名为program的类。然后调用 main 方法,其中使用字符串变量来存储字符串,检查该字符串以查找其中是否存在另一个字符串。然后使用另一个字符串变量来存储在第一个字符串中搜索其出现的字符串。然后,再次使用另一个字符串变量来存储在第一个字符串中搜索其出现的字符串。然后使用 Contains() 方法检查字符串中是否存在另一个字符串。如果第一个字符串中出现了另一个字符串,则返回 True;如果其中没有出现另一个字符串,则返回 false。

结论

在本教程中,我们通过编程示例及其输出了解 Contains() 方法的定义、Contains() 方法的语法以及 Contains() 方法的工作原理,了解 C# 中 Contains() 方法的概念。

推荐文章

这是 C# Contains() 的指南。在这里,我们讨论 C# Contains() 方法的介绍及其工作原理以及示例和代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –

  1. C# 中的随机数生成器是什么?
  2. Java 中的静态构造函数 |工作|应用
  3. C# 中的 TextWriter |示例
  4. 如何在 C# 中使用静态构造函数?

以上是C# 包含()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# String Split()下一篇:C# DirectoryInfo