String.Contains Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Přetížení
| Name | Description |
|---|---|
| Contains(Rune, StringComparison) | |
| Contains(String, StringComparison) |
Vrátí hodnotu určující, zda se zadaný řetězec vyskytuje v tomto řetězci pomocí zadaných srovnávacích pravidel. |
| Contains(Char, StringComparison) |
Vrátí hodnotu určující, zda se zadaný znak vyskytuje v tomto řetězci pomocí zadaných srovnávacích pravidel. |
| Contains(String) |
Vrátí hodnotu určující, zda se zadaný podřetězec vyskytuje v rámci tohoto řetězce. |
| Contains(Char) |
Vrátí hodnotu určující, zda se v tomto řetězci vyskytuje zadaný znak. |
| Contains(Rune) |
Contains(Rune, StringComparison)
public:
bool Contains(System::Text::Rune value, StringComparison comparisonType);
public bool Contains(System.Text.Rune value, StringComparison comparisonType);
member this.Contains : System.Text.Rune * StringComparison -> bool
Public Function Contains (value As Rune, comparisonType As StringComparison) As Boolean
Parametry
- value
- Rune
- comparisonType
- StringComparison
Návraty
Platí pro
Contains(String, StringComparison)
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
Vrátí hodnotu určující, zda se zadaný řetězec vyskytuje v tomto řetězci pomocí zadaných srovnávacích pravidel.
public:
bool Contains(System::String ^ value, StringComparison comparisonType);
public bool Contains(string value, StringComparison comparisonType);
member this.Contains : string * StringComparison -> bool
Public Function Contains (value As String, comparisonType As StringComparison) As Boolean
Parametry
- value
- String
Řetězec, který chcete vyhledat.
- comparisonType
- StringComparison
Jedna z hodnot výčtu, která určuje pravidla, která se mají použít při porovnání.
Návraty
true
value pokud se parametr vyskytuje v tomto řetězci nebo pokud value je prázdný řetězec (""); v opačném případě . false
Platí pro
Contains(Char, StringComparison)
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
Vrátí hodnotu určující, zda se zadaný znak vyskytuje v tomto řetězci pomocí zadaných srovnávacích pravidel.
public:
bool Contains(char value, StringComparison comparisonType);
public bool Contains(char value, StringComparison comparisonType);
member this.Contains : char * StringComparison -> bool
Public Function Contains (value As Char, comparisonType As StringComparison) As Boolean
Parametry
- value
- Char
Znak, který se má hledat.
- comparisonType
- StringComparison
Jedna z hodnot výčtu, která určuje pravidla, která se mají použít při porovnání.
Návraty
true
value pokud se parametr vyskytuje v tomto řetězci, jinak . false
Platí pro
Contains(String)
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
Vrátí hodnotu určující, zda se zadaný podřetězec vyskytuje v rámci tohoto řetězce.
public:
bool Contains(System::String ^ value);
public bool Contains(string value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean
Parametry
- value
- String
Řetězec, který chcete vyhledat.
Návraty
true
value pokud se parametr vyskytuje v tomto řetězci nebo pokud value je prázdný řetězec (""); v opačném případě . false
Výjimky
value je null.
Příklady
Následující příklad určuje, zda řetězec "fox" je podřetězce známé uvozovky. Pokud se v řetězci najde "fox", zobrazí se také počáteční pozice.
string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b = s1.Contains(s2);
Console.WriteLine("'{0}' is in the string '{1}': {2}",
s2, s1, b);
if (b) {
int index = s1.IndexOf(s2);
if (index >= 0)
Console.WriteLine("'{0} begins at character position {1}",
s2, index + 1);
}
// This example displays the following output:
// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
// 'fox begins at character position 17
let s1 = "The quick brown fox jumps over the lazy dog"
let s2 = "fox"
let b = s1.Contains s2
printfn $"'{s2}' is in the string '{s1}': {b}"
if b then
let index = s1.IndexOf s2
if index >= 0 then
printfn $"'{s2} begins at character position {index + 1}"
// This example displays the following output:
// 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
// 'fox begins at character position 17
Class Example
Public Shared Sub Main()
Dim s1 As String = "The quick brown fox jumps over the lazy dog"
Dim s2 As String = "fox"
Dim b As Boolean = s1.Contains(s2)
Console.WriteLine("'{0}' is in the string '{1}': {2}",
s2, s1, b)
If b Then
Dim index As Integer = s1.IndexOf(s2)
If index >= 0 Then
Console.WriteLine("'{0} begins at character position {1}",
s2, index + 1)
End If
End If
End Sub
End Class
'
' This example displays the following output:
' 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
' 'fox begins at character position 17
Poznámky
Tato metoda provádí porovnání řadových (nerozlišující velká a malá písmena a nerozlišují se podle jazykové verze). Hledání začíná na první pozici znaku tohoto řetězce a pokračuje přes poslední pozici znaku.
Chcete-li provést porovnání nerozlišující velká a malá písmena v jazykové verzi:
V .NET Core 2.1 a novějších verzích: Místo toho zavolejte Contains(String, StringComparison) přetížení.
V rozhraní .NET Framework: Vytvořte vlastní metodu. Následující příklad ukazuje jeden takový přístup. Definuje rozšiřující metodu StringStringComparison , která obsahuje parametr a označuje, zda řetězec obsahuje podřetězce při použití zadané formy porovnání řetězců.
using System;
public static class StringExtensions
{
public static bool Contains(this String str, String substring,
StringComparison comp)
{
if (substring == null)
throw new ArgumentNullException("substring",
"substring cannot be null.");
else if (!Enum.IsDefined(typeof(StringComparison), comp))
throw new ArgumentException("comp is not a member of StringComparison",
"comp");
return str.IndexOf(substring, comp) >= 0;
}
}
open System
open System.Runtime.CompilerServices
[<Extension>]
type StringExtensions =
[<Extension>]
static member Contains(str: string, substring, comp: StringComparison) =
if substring = null then
invalidArg "substring" "substring cannot be null"
if Enum.IsDefined(typeof<StringComparison>, comp) |> not then
invalidArg "comp" "comp is not a member of StringComparison"
str.IndexOf(substring, comp) >= 0
String s = "This is a string.";
String sub1 = "this";
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
StringComparison comp = StringComparison.Ordinal;
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
comp = StringComparison.OrdinalIgnoreCase;
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp));
// The example displays the following output:
// Does 'This is a string.' contain 'this'?
// Ordinal: False
// OrdinalIgnoreCase: True
let s = "This is a string."
let sub1 = "this"
printfn $"Does '{s}' contain '{sub1}'?"
let comp = StringComparison.Ordinal
printfn $" {comp:G}: {s.Contains(sub1, comp)}"
let comp2 = StringComparison.OrdinalIgnoreCase
printfn $" {comp2:G}: {s.Contains(sub1, comp2)}"
// The example displays the following output:
// Does 'This is a string.' contain 'this'?
// Ordinal: False
// OrdinalIgnoreCase: True
Imports System.Runtime.CompilerServices
Module StringExtensions
<Extension()>
Public Function Contains(str As String, substring As String,
comp As StringComparison) As Boolean
If substring Is Nothing Then
Throw New ArgumentNullException("substring",
"substring cannot be null.")
Else If Not [Enum].IsDefined(GetType(StringComparison), comp)
Throw New ArgumentException("comp is not a member of StringComparison",
"comp")
End If
Return str.IndexOf(substring, comp) >= 0
End Function
End Module
Public Module Example
Public Sub Main
Dim s As String = "This is a string."
Dim sub1 As String = "this"
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1)
Dim comp As StringComparison = StringComparison.Ordinal
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp))
comp = StringComparison.OrdinalIgnoreCase
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp))
End Sub
End Module
' The example displays the following output:
' Does 'This is a string.' contain 'this'?
' Ordinal: False
' OrdinalIgnoreCase: True
Pokud vás zajímá pozice podřetězce value v aktuální instanci, můžete metodu IndexOf volat, abyste získali počáteční pozici jejího prvního výskytu, nebo můžete volat metodu LastIndexOf , která získá počáteční pozici jejího posledního výskytu. Příklad obsahuje volání IndexOf(String) metody, pokud je v instanci řetězce nalezen podřetězce.
Viz také
Platí pro
Contains(Char)
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
- Zdroj:
- String.Searching.cs
Vrátí hodnotu určující, zda se v tomto řetězci vyskytuje zadaný znak.
public:
bool Contains(char value);
public bool Contains(char value);
member this.Contains : char -> bool
Public Function Contains (value As Char) As Boolean
Parametry
- value
- Char
Znak, který se má hledat.
Návraty
true
value pokud se parametr vyskytuje v tomto řetězci, jinak . false
Poznámky
Tato metoda provádí porovnání řadových (nerozlišující velká a malá písmena a nerozlišují se podle jazykové verze).