2011-05-01 164 views
24

希望有人能幫助我。我只是在學習C#,我有一個簡單的問題。如何檢查字符串是否存在於另一個字符串中

我有一個變量,我想檢查是否存在另一個字符串。喜歡的東西

if (test contains "abc") { 

} 

有一個簡單的方法來做到這在C#

+0

http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx – Marlon 2011-05-01 12:31:02

+1

Geepers ......你幾乎回答了這個問題你的問題。使用API​​文檔Luke ... force就是sooo 90's – corlettk 2011-05-01 12:36:13

回答

49

使用String.Contains

if (stringValue.Contains(anotherStringValue)) 
{ 
    // Do Something // 
} 
+0

請參閱下面的答案以區分大小寫不同的變體 – Pellet 2017-09-06 02:24:29

10

IndexOf()功能將做的工作...
它將返回-1,如果字符串不存在

4
string MainString = "String Manipulation"; 
string SearchString = "pul"; 
int FirstChr = MainString.IndexOf(SearchString); 

此代碼顯示如何在字符串內搜索子字符串,並返回開始的索引位置或指示字符串未找到的-1。

您還可以使用Contains(),Contains是字符串類型的實例方法,這意味着您可以在程序中的特定字符串上調用它。它有一個布爾結果,如果找到該參數,則返回true;如果未找到,則返回false。

using System; 

class Program 
{ 
    static void Main() 
    { 
    Test("Dot Net Perls"); 
    Test("dot net perls"); 
    } 

    static void Test(string input) 
    { 
    Console.Write("--- "); 
    Console.Write(input); 
    Console.WriteLine(" ---"); 
    // 
    // See if the string contains 'Net' 
    // 
    bool contains = input.Contains("Net"); 
    // 
    // Write the result 
    // 
    Console.Write("Contains 'Net': "); 
    Console.WriteLine(contains); 
    // 
    // See if the string contains 'perls' lowercase 
    // 
    if (input.Contains("perls")) 
    { 
     Console.WriteLine("Contains 'perls'"); 
    } 
    // 
    // See if the string contains 'Dot' 
    // 
    if (!input.Contains("Dot")) 
    { 
     Console.WriteLine("Doesn't Contain 'Dot'"); 
    } 
    } 
} 

查看C# String Functions and Manipulation瞭解有關字符串的任何信息。

3

您必須使用正則表達式。例如Regex.IsMatch(test, "abc")。如果測試包含abc,這將返回true。

+5

正如您在上面看到的,您不需要使用正則表達式。事實上,這種方式可能是最鈍的,最慢的,並且最難理解它們的方式。 – VoidKing 2012-12-21 21:57:49

3

使用String.Contains(...)可能不是一個好主意。

String.Contains(...)做了一個有序的區分大小寫的比較。所以,小心大小寫匹配。

ofcourse你可以使用ToLower()ToUpper()您檢查

2

使用可以使用String.Contains此之前。

if (test.Contains("abc")) 
{ 
    // Your Code Here 
} 
3
if (stringValue.ToUpper().Contains("FIND_THIS")) 
{ 
    // Do Something // 
} 

是不區分大小寫的搜索另一個很好的變化。

0

有一些方法可以做到這一點的驗證像CompareToContainsCompareIndexOfAnyIndexOf

檢查String類的方法列表。

string s1 = "ani\u00ADmal"; 
string s2 = "animal"; 
string s3 = "abc"; 
string s4 = "abc"; 
string s5 = "ABC"; 

bool b1 = s1.CompareTo(s2) > -1; // return true, exists 
bool b2 = s3.CompareTo(s4) > -1; // return true, exists 
bool b3 = s3.CompareTo(s5) > -1; // return false, no case sensitive, no exists 

bool b4 = s1.Contains(s2); // return false, no exists 
bool b5 = s3.Contains(s4); // return true, exists 
bool b6 = s3.Contains(s5); // return false, no case sensitive, no exists 

string s6 = "MACHINE"; 
string s7 = "machine"; 
string s8 = "nature"; 

int a = String.Compare(s6, 0, s7, 0, s6.Length, true); // return 0, contain and is less 
int b = String.Compare(s6, 0, s7, 0, s6.Length, false); // return 1, contain and is greater 
int c = String.Compare(s6, 0, s8, 0, s6.Length, true); // return -1, no contain 
int d = String.Compare(s6, 0, s8, 0, s6.Length, false); // return -1, no contain 

int e = s1.IndexOfAny(s2.ToCharArray()); // return 0, exists 
int f = s3.IndexOfAny(s4.ToCharArray()); // return 0, exists 
int g = s3.IndexOfAny(s5.ToCharArray()); // return -1, no case sensitive, no exists 

int h = s1.IndexOf(s2); // return 0, exists 
int i = s3.IndexOf(s4); // return 0, exists 
int j = s3.IndexOf(s5); // return -1, no exists 
相關問題