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 
相关问题