C# contains方法支持正则吗

   2024-10-06 4460
核心提示:C# 的 Contains() 方法不直接支持正则表达式。如果您想要使用正则表达式来确定字符串是否包含某个模式,可以使用 Regex 类来实现

C# 的 Contains() 方法不直接支持正则表达式。如果您想要使用正则表达式来确定字符串是否包含某个模式,可以使用 Regex 类来实现。您可以通过创建一个 Regex 对象,然后使用它的 Match() 方法来检查字符串是否包含与正则表达式匹配的内容。以下是一个简单的示例:

using System;using System.Text.RegularExpressions;class Program{    static void Main()    {        string input = "Hello, world!";        string pattern = @"world";        Regex regex = new Regex(pattern);        bool containsPattern = regex.IsMatch(input);        if (containsPattern)        {            Console.WriteLine("Input string contains the pattern.");        }        else        {            Console.WriteLine("Input string does not contain the pattern.");        }    }}

这段代码演示了如何使用 Regex 类来检查输入字符串是否包含指定的正则表达式模式。您可以根据需要修改正则表达式模式以匹配您想要检查的内容。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号