當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。