📜  C#| String.Contains()方法

📅  最后修改于: 2021-05-30 01:35:53             🧑  作者: Mango

在C#中, String.Contains()是一个字符串方法。此方法用于检查子字符串是否出现在给定的字符串内。

句法:

public bool Contains(string str)

范围:

返回值:返回布尔值。如果子字符串存在于字符串,或者value是空字符串(“”),则返回True,否则返回False。

异常:如果strnull,则此方法可以提供ArgumentNullException

注意:此方法执行区分大小写的检查。搜索将始终从字符串的第一个字符位置开始,一直持续到最后一个字符位置。

下面的程序说明了Contains()方法。

程序1:

// C# program to demonstrate the
// String.Contains() Method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // string type
        String str = "GeeksforGeeks";
        String substr1 = "for";
        String substr2 = "For";
  
        // using String.Contains() Method
        Console.WriteLine(str.Contains(substr1));
  
        // Here case-sensitive comparison
        // And substr2 value is 'For'
        // So its return false
        Console.WriteLine(str.Contains(substr2));
    }
}
输出:
True
False

程序2:使用顺序比较和不区分大小写的顺序比较来确定字符串中是否存在子字符串。

// C# program to illustrate the
// String.Contains() Method using
// ordinal comparison and case-
// insensitive ordinal comparison
using System;
  
public static class StringExtensions {
  
    // defines a String extension method
    // which includes a StringComparison parameter
    public static bool Contains(this String str,
                                String substr,
                                StringComparison cmp)
    {
        if (substr == null)
            throw new ArgumentNullException("substring substring",
                                            " cannot be null.");
  
        else if (!Enum.IsDefined(typeof(StringComparison), cmp))
            throw new ArgumentException("comp is not a member of",
                                        "StringComparison, comp");
  
        return str.IndexOf(substr, cmp) >= 0;
    }
}
  
// Driver Class
class Geeks {
  
    // Main Method
    public static void Main()
    {
        String str = "GeeksforGeeks";
        String substr = "FOR";
  
        // For Ordinal
        StringComparison comp = StringComparison.Ordinal;
        Console.WriteLine("For {0:G}: {1}", comp,
                          str.Contains(substr, comp));
  
        // for OrdinalIgnoreCase
        comp = StringComparison.OrdinalIgnoreCase;
        Console.WriteLine("For {0:G}: {1}", comp,
                          str.Contains(substr, comp));
    }
}
输出:
For Ordinal: False
For OrdinalIgnoreCase: True

程序3:下面的示例确定字符串“ Computer”是否为给定字符串的子字符串。如果在字符串找到它,它还将显示其起始位置。

// C# program to demonstrate the
// String.Contains() Method
// along with the starting position
using System;
  
class Example {
    public static void Main()
    {
        string sub1 = "GeeksforGeeks is a Computer Science Portal";
        string sub2 = "Computer";
  
        // Check if the substring is
        // present in the main string
        bool b = sub1.Contains(sub2);
  
        Console.WriteLine("'{0}' is in the string '{1}': {2}",
                          sub2, sub1, b);
        if (b) {
            int index = sub1.IndexOf(sub2);
            if (index >= 0)
                Console.WriteLine("{0} begins at character position {1}",
                                  sub2, index + 1);
        }
    }
}
输出:
'Computer' is in the string 'GeeksforGeeks is a Computer Science Portal': True
Computer begins at character position 20

参考: https : //msdn.microsoft.com/en-us/library/system。字符串。包含