【问题标题】:How can I check if a string exists in another string如何检查一个字符串是否存在于另一个字符串中
【发布时间】:2011-05-01 12:27:28
【问题描述】:

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

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

if ( test contains "abc" ) {

}

在 C# 中有没有简单的方法来做到这一点

【问题讨论】:

标签: c#


【解决方案1】:

使用String.Contains:

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

【讨论】:

  • 在下面查看我的答案以了解不区分大小写的变化
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    string MainString = "String Manipulation"; 
    string SearchString = "pul"; 
    int FirstChr = MainString.IndexOf(SearchString); 
    

    此代码显示如何在字符串中搜索子字符串,并返回开始的索引位置或表示未找到该字符串的 -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 了解有关字符串的任何信息。

    【讨论】:

      【解决方案4】:

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

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

      当然你可以在检查前使用ToLower()ToUpper()

      【讨论】:

        【解决方案5】:
        if (stringValue.ToUpper().Contains("FIND_THIS"))
        {  
            // Do Something // 
        } 
        

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

        【讨论】:

          【解决方案6】:

          请参考This

          String.Contains(...)
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

            • 正如您在上面看到的,您“不需要”使用正则表达式。事实上,这种方式可能是所有方式中最迟钝、最慢、最难理解的方式。
            【解决方案8】:

            为此可以使用String.Contains

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

            【讨论】:

              【解决方案9】:

              有一些方法可以进行此验证,例如 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
              

              【讨论】:

                【解决方案10】:

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

                if(string1.ToUpper().Contains(string2.ToUpper())
                {
                    //Your code goes here
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-03-24
                  • 1970-01-01
                  • 2010-10-13
                  • 2022-03-31
                  • 2013-05-12
                  相关资源
                  最近更新 更多