簡體   English   中英

如何在C#中使用通配符模式進行字符串比較

[英]How to do string comparison with wildcard pattern in C#

C#是否提供了將字符串與通配符模式進行比較的任何方法。 或者我可以說我想找一個“Like Operator”來進行字符串比較。 假設我有一個字符串。我也有一個段落,我想找到這個parapgraph上的字符串,但是怎么做。在SQL中我們只能使用LIKE運算符。

任何建議和回復都很感激。

通配符是一個復雜的野獸(一種正則表達式),但聽起來你想要Contains方法。 你可以做paragraph.Contains(sentence)

String有一個應該足夠的Contains方法,返回一個boolean

"Big string that represents a paragraph".Contains("that");

包含方法MSDN頁面中的示例:

// This example demonstrates the String.Contains() method
using System;

class Sample 
{
    public static void Main() 
    {
    string s1 = "The quick brown fox jumps over the lazy dog";
    string s2 = "fox";
    bool b;
    b = s1.Contains(s2);
    Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);
    }
}
/*
This example produces the following results:

Is the string, s2, in the string, s1?: True
*/

如果你需要更高級的匹配,那么Regex可能是正確的路線,但是從你說的我認為包含的例子中可以正常工作。

您可以使用Regex定義通配符。 這些不像DOS那樣完全有效,但功能更強大。 看到:

http://msdn.microsoft.com/en-us/library/ms228595(VS.80).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM