StringCollection.Insert メソッド
アセンブリ: System (system.dll 内)

Dim instance As StringCollection Dim index As Integer Dim value As String instance.Insert(index, value)


StringCollection では、文字列を重複して使用できます。
index と Count が等しい場合は、value が StringCollection の末尾に追加されます。
リストなどの連続する要素のコレクションでは、新しい要素を挿入するために、挿入位置より後にある要素の位置が繰り下げられます。インデックス付きのコレクションの場合は、移動した要素のインデックスも更新されます。この動作は、要素が概念的にバケットにグループ化されているハッシュ テーブルなどのコレクションには適用されません。

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) 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: ' '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 '
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 ); } 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: 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 */
#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; Console::WriteLine( "Initial contents of the StringCollection:" ); PrintValues( myCol ); // Adds a range of elements from an array to the end of the StringCollection. array<String^>^myArr = {"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 ); } 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: 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 */
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(); 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); } //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: 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 */

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からStringCollection.Insert メソッドを検索する場合は、下記のリンクをクリックしてください。

- StringCollection.Insert メソッドのページへのリンク