StringCollection クラス
文字列のコレクションを表します。
この型のすべてのメンバの一覧については、StringCollection メンバ を参照してください。
System.Object
System.Collections.Specialized.StringCollection
<Serializable>
Public Class StringCollection Implements IList, ICollection, IEnumerable
[C#]
[Serializable]
public class StringCollection : IList, ICollection, IEnumerable
[C++]
[Serializable]
public __gc class StringCollection : public IList, ICollection, IEnumerable
[JScript]
public
Serializable
class StringCollection implements IList, ICollection, IEnumerable
スレッドセーフ
この型の public static (Visual Basic では Shared) メンバは、マルチスレッド操作に対して安全です。インスタンス メンバがスレッド セーフになるかどうかは保証されていません。
この実装は、 StringCollection 用の同期された (スレッド セーフな) ラッパーは提供しませんが、派生クラスでは、 SyncRoot プロパティを使用して、同期した StringCollection を独自に作成できます。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチする方法のいずれかを実行できます。
解説
StringCollection は、 null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。
文字列比較では、大文字と小文字が区別されます。
このコレクションのインデックスは 0 から始まります。
使用例
[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()
Console.WriteLine("Initial contents of the StringCollection:")
PrintValues(myCol)
' Adds a range of elements from an array to the end of the StringCollection.
Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
myCol.AddRange(myArr)
Console.WriteLine("After adding a range of elements:")
PrintValues(myCol)
' Adds one element to the end of the StringCollection and inserts another at index 3.
myCol.Add("* white")
myCol.Insert(3, "* gray")
Console.WriteLine("After adding ""* white"" to the end and inserting ""* gray"" at index 3:")
PrintValues(myCol)
' Removes one element from the StringCollection.
myCol.Remove("yellow")
Console.WriteLine("After removing ""yellow"":")
PrintValues(myCol)
' Removes all occurrences of a value from the StringCollection.
Dim i As Integer = myCol.IndexOf("RED")
While i > - 1
myCol.RemoveAt(i)
i = myCol.IndexOf("RED")
End While
' Verifies that all occurrences of "RED" are gone.
If myCol.Contains("RED") Then
Console.WriteLine("*** The collection still contains ""RED"".")
End If
Console.WriteLine("After removing all occurrences of ""RED"":")
PrintValues(myCol)
' Copies the collection to a new array starting at index 0.
Dim myArr2(myCol.Count) As [String]
myCol.CopyTo(myArr2, 0)
Console.WriteLine("The new array contains:")
For i = 0 To myArr2.Length - 1
Console.WriteLine(" [{0}] {1}", i, myArr2(i))
Next i
Console.WriteLine()
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("After clearing the collection:")
PrintValues(myCol)
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:
'
'After adding a range of elements:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'After adding "* white" to the end and inserting "* gray" at index 3:
' RED
' orange
' yellow
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing "yellow":
' RED
' orange
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing all occurrences of "RED":
' orange
' * gray
' green
' blue
' indigo
' violet
' * white
'
'The new array contains:
' [0] orange
' [1] * gray
' [2] green
' [3] blue
' [4] indigo
' [5] violet
' [6] * white
'
'After clearing the collection:
'
[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();
Console.WriteLine( "Initial contents of the StringCollection:" );
PrintValues( myCol );
// Adds a range of elements from an array to the end of the StringCollection.
String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
myCol.AddRange( myArr );
Console.WriteLine( "After adding a range of elements:" );
PrintValues( myCol );
// Adds one element to the end of the StringCollection and inserts another at index 3.
myCol.Add( "* white" );
myCol.Insert( 3, "* gray" );
Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
PrintValues( myCol );
// Removes one element from the StringCollection.
myCol.Remove( "yellow" );
Console.WriteLine( "After removing \"yellow\":" );
PrintValues( myCol );
// Removes all occurrences of a value from the StringCollection.
int i = myCol.IndexOf( "RED" );
while ( i > -1 ) {
myCol.RemoveAt( i );
i = myCol.IndexOf( "RED" );
}
// Verifies that all occurrences of "RED" are gone.
if ( myCol.Contains( "RED" ) )
Console.WriteLine( "*** The collection still contains \"RED\"." );
Console.WriteLine( "After removing all occurrences of \"RED\":" );
PrintValues( myCol );
// Copies the collection to a new array starting at index 0.
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo( myArr2, 0 );
Console.WriteLine( "The new array contains:" );
for ( i = 0; i < myArr2.Length; i++ ) {
Console.WriteLine( " [{0}] {1}", i, myArr2[i] );
}
Console.WriteLine();
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "After clearing the collection:" );
PrintValues( myCol );
}
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:
After adding a range of elements:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
After adding "* white" to the end and inserting "* gray" at index 3:
RED
orange
yellow
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing "yellow":
RED
orange
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing all occurrences of "RED":
orange
* gray
green
blue
indigo
violet
* white
The new array contains:
[0] orange
[1] * gray
[2] green
[3] blue
[4] indigo
[5] violet
[6] * white
After clearing the collection:
*/
[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();
Console::WriteLine(S"Initial contents of the StringCollection:");
PrintValues(myCol);
// Adds a range of elements from an array to the end of the 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"After adding a range of elements:");
PrintValues(myCol);
// Adds one element to the end of the StringCollection and inserts another at index 3.
myCol->Add(S"* white");
myCol->Insert(3, S"* gray");
Console::WriteLine(S"After adding \"* white\" to the end and inserting \"* gray\" at index 3:");
PrintValues(myCol);
// Removes one element from the StringCollection.
myCol->Remove(S"yellow");
Console::WriteLine(S"After removing \"yellow\":");
PrintValues(myCol);
// Removes all occurrences of a value from the StringCollection.
int i = myCol->IndexOf(S"RED");
while (i > -1) {
myCol->RemoveAt(i);
i = myCol->IndexOf(S"RED");
}
// Verifies that all occurrences of S"RED" are gone.
if (myCol->Contains(S"RED"))
Console::WriteLine(S"*** The collection still contains \"RED\".");
Console::WriteLine(S"After removing all occurrences of \"RED\":");
PrintValues(myCol);
// Copies the collection to a new array starting at index 0.
String* myArr2[] = new String*[myCol->Count];
myCol->CopyTo(myArr2, 0);
Console::WriteLine(S"The new array contains:");
for (i = 0; i < myArr2->Length; i++) {
Console::WriteLine(S" [{0}] {1}", __box(i), myArr2->Item[i]);
}
Console::WriteLine();
// Clears the entire collection.
myCol->Clear();
Console::WriteLine(S"After clearing the collection:");
PrintValues(myCol);
}
/*
This code produces the following output.
Initial contents of the StringCollection:
After adding a range of elements:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
After adding "* white" to the end and inserting "* gray" at index 3:
RED
orange
yellow
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing "yellow":
RED
orange
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing all occurrences of "RED":
orange
* gray
green
blue
indigo
violet
* white
The new array contains:
[0] orange
[1] * gray
[2] green
[3] blue
[4] indigo
[5] violet
[6] * white
After clearing the collection:
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Collections.Specialized
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: System (System.dll 内)
参照
StringCollection メンバ | System.Collections.Specialized 名前空間 | カルチャを認識しない文字列操作の実行