如何检查另一个字符串中是否存在字符串

时间:2011-05-01 12:27:28

标签: c#

希望有人可以帮助我。我刚刚学习C#,我有一个简单的问题。

我有一个变量,我想检查是否存在于另一个字符串中。像

这样的东西
if ( test contains "abc" ) {

}

在C#中有一种简单的方法吗?

11 个答案:

答案 0 :(得分:66)

使用String.Contains

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

答案 1 :(得分:11)

IndexOf()函数将完成工作...
如果字符串不存在,它将返回-1

答案 2 :(得分:4)

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

此代码显示如何在字符串中搜索子字符串,并返回start的索引位置或-1,表示尚未找到该字符串。

您也可以使用Contains(),Contains是字符串类型的实例方法,这意味着您可以在程序中的特定字符串上调用它。它有一个bool结果,如果找到参数则为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 :(得分:4)

参考This

String.Contains(...)

答案 4 :(得分:4)

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

String.Contains(...)执行序数区分大小写的比较。因此,请注意案例匹配。

当然,您可以在检查之前使用ToLower()ToUpper()

答案 5 :(得分:3)

您必须使用正则表达式。例如Regex.IsMatch(test, "abc")。如果test包含abc,则返回true。

答案 6 :(得分:3)

if (stringValue.ToUpper().Contains("FIND_THIS"))
{  
    // Do Something // 
} 

对于不区分大小写的搜索,这是另一个很好的变体。

答案 7 :(得分:2)

使用可以使用String.Contains

if (test.Contains("abc"))
{  
    // Your Code Here
}

答案 8 :(得分: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

答案 9 :(得分:0)

如果您需要相反的操作并检查另一个字符串中是否不存在一个字符串,只需输入!即可。在条件前面标记:

<input type="checkbox" name="@T("WholeSale")" id="chb_wholesale" onclick="setLocation('@Url.RouteUrl("Vendor", new { vendorId = Model.Id, categoryId = Model.CategoryId, wholesale = xxx })')" />

答案 10 :(得分:0)

您可以使用.Contains,但是也可以使用ToUpper(),因为.Contains区分大小写。

如果(string1.ToUpper()。包含(string2.ToUpper()) {

}

相关问题