当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# String.Contains()用法及代码示例


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

用法:

public bool Contains(string str)

参数:


  • str:这是要检查的字符串。此参数的类型为System.String。

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

异常:如果str为null,则此方法可以提供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.string.contains



相关用法


注:本文由纯净天空筛选整理自Mithun Kumar大神的英文原创作品 C# | String.Contains() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。