Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Bestimmt, ob sich die angegebene Zeichenfolge in der StringCollection befindet.
Namespace: System.Collections.Specialized
Assembly: System (in system.dll)
Syntax
'Declaration
Public Function Contains ( _
value As String _
) As Boolean
'Usage
Dim instance As StringCollection
Dim value As String
Dim returnValue As Boolean
returnValue = instance.Contains(value)
public bool Contains (
string value
)
public:
bool Contains (
String^ value
)
public boolean Contains (
String value
)
public function Contains (
value : String
) : boolean
Parameter
- value
Die in der StringCollection zu suchende Zeichenfolge. Der Wert kann NULL (Nothing in Visual Basic) sein.
Rückgabewert
true, wenn sich value in StringCollection befindet; andernfalls false.
Hinweise
Mit der Contains-Methode kann vor dem Ausführen anderer Operationen das Vorhandensein einer Zeichenfolge bestätigt werden.
Diese Methode ruft Object.Equals für die Gleichheitsprüfung auf. Bei Zeichenfolgenvergleichen wird die Groß- und Kleinschreibung berücksichtigt.
Diese Methode führt eine lineare Suche durch. Daher ist diese Methode eine O(n)-Operation, wobei n gleich Count ist.
Beispiel
Im folgenden Codebeispiel wird ein Element in der StringCollection gesucht.
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringCollection
Public Shared Sub Main()
' Creates and initializes a new StringCollection.
Dim myCol As New StringCollection()
Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
myCol.AddRange(myArr)
Console.WriteLine("Initial contents of the StringCollection:")
PrintValues(myCol)
' Checks whether the collection contains "orange" and, if so, displays its index.
If myCol.Contains("orange") Then
Console.WriteLine("The collection contains ""orange"" at index {0}.", myCol.IndexOf("orange"))
Else
Console.WriteLine("The collection does not contain ""orange"".")
End If
End Sub 'Main
Public Shared Sub PrintValues(myCol As IEnumerable)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues
End Class 'SamplesStringCollection
'This code produces the following output.
'
'Initial contents of the StringCollection:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'The collection contains "orange" at index 1.
'
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
// Creates and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
myCol.AddRange( myArr );
Console.WriteLine( "Initial contents of the StringCollection:" );
PrintValues( myCol );
// Checks whether the collection contains "orange" and, if so, displays its index.
if ( myCol.Contains( "orange" ) )
Console.WriteLine( "The collection contains \"orange\" at index {0}.", myCol.IndexOf( "orange" ) );
else
Console.WriteLine( "The collection does not contain \"orange\"." );
}
public static void PrintValues( IEnumerable myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
Initial contents of the StringCollection:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
The collection contains "orange" at index 1.
*/
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
// Creates and initializes a new StringCollection.
StringCollection^ myCol = gcnew StringCollection;
array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
myCol->AddRange( myArr );
Console::WriteLine( "Initial contents of the StringCollection:" );
PrintValues( myCol );
// Checks whether the collection contains "orange" and, if so, displays its index.
if ( myCol->Contains( "orange" ) )
Console::WriteLine( "The collection contains \"orange\" at index {0}.", myCol->IndexOf( "orange" ) );
else
Console::WriteLine( "The collection does not contain \"orange\"." );
}
void PrintValues( IEnumerable^ myCol )
{
IEnumerator^ myEnum = myCol->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( " {0}", obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
Initial contents of the StringCollection:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
The collection contains "orange" at index 1.
*/
import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;
public class SamplesStringCollection
{
public static void main(String[] args)
{
// Creates and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
String myArr[] = new String[]{"RED", "orange", "yellow", "RED",
"green", "blue", "RED", "indigo", "violet", "RED"};
myCol.AddRange(myArr);
Console.WriteLine("Initial contents of the StringCollection:");
PrintValues(myCol);
// Checks whether the collection contains "orange" and, if so,
// displays its index.
if ( myCol.Contains("orange")) {
Console.WriteLine("The collection contains \"orange\" at index"
+ " {0}.",System.Convert.ToString(myCol.IndexOf("orange")));
}
else {
Console.WriteLine("The collection does not contain \"orange\".");
}
} //main
public static void PrintValues(IEnumerable myCol)
{
Object obj = null;
IEnumerator objEnum = myCol.GetEnumerator();
while (objEnum.MoveNext()) {
obj = objEnum.get_Current();
Console.WriteLine(" {0}", obj);
}
Console.WriteLine();
} //PrintValues
} //SamplesStringCollection
/*
This code produces the following output.
Initial contents of the StringCollection:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
The collection contains "orange" at index 1.
*/
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
StringCollection-Klasse
StringCollection-Member
System.Collections.Specialized-Namespace
IndexOf