指定した文字列が StringCollection 内にあるかどうかを確認します。
Public Function Contains( _
ByVal value As String _) As Boolean
[C#]
public bool Contains(stringvalue);
[C++]
public: bool Contains(String* value);
[JScript]
public function Contains(
value : String) : Boolean;
パラメータ
- value
StringCollection 内で検索する文字列。値は null 参照 (Visual Basic では Nothing) に設定できます。
戻り値
value が StringCollection に存在する場合は true 。それ以外の場合は false 。
解説
Contains メソッドを使用すると、操作を続行する前に、指定した文字列が存在するかどうかを確認できます。
このメソッドは順次検索を実行します。したがって、平均検索時間は Count に比例します。つまり、このメソッドは O(n) 操作です。ここで、 n は Count です。
このメソッドは、 Object.Equals を呼び出して、等しいかどうかを判断します。文字列比較では、大文字と小文字が区別されます。
使用例
[Visual Basic, C#, C++] StringCollection の要素を検索する例を次に示します。
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 myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(" {0}", myEnumerator.Current)
End While
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.
'
[C#]
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 ) {
System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( " {0}", myEnumerator.Current );
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.
*/
[C++]
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues(IEnumerable* myCol) {
System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
while (myEnumerator->MoveNext())
Console::WriteLine(S" {0}", myEnumerator->Current);
Console::WriteLine();
}
int main() {
// Creates and initializes a new StringCollection.
StringCollection* myCol = new StringCollection();
String* myArr[] = { S"RED", S"orange", S"yellow", S"RED", S"green", S"blue", S"RED", S"indigo", S"violet", S"RED" };
myCol->AddRange(myArr);
Console::WriteLine(S"Initial contents of the StringCollection:");
PrintValues(myCol);
// Checks whether the collection contains S"orange" and, if so, displays its index.
if (myCol->Contains(S"orange"))
Console::WriteLine(S"The collection contains \"orange\" at index {0}.", __box(myCol->IndexOf(S"orange")));
else
Console::WriteLine(S"The collection does not contain \"orange\".");
}
/*
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.
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
StringCollection クラス | StringCollection メンバ | System.Collections.Specialized 名前空間 | IndexOf | カルチャを認識しない文字列操作の実行