Visual C++ .NET/Collections/StringCollection

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add a range to StringCollection

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
   strcol->AddRange(tmpstr);

}

 </source>


Add keys from a StringDictionary to an ArrayList

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";
   
   ArrayList ^alist = gcnew ArrayList();
   alist->AddRange(strdict->Keys);
   alist->Sort();

}

 </source>


Add key value pair to StringDictionary

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");

}

 </source>


Add string to StringCollection

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   strcol->Add("The first String");

}

 </source>


Assign string value to StringCollection by indexer

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
   strcol->AddRange(tmpstr);
   strcol[0] = "First";
   strcol->Insert(1, "Second");

}

 </source>


Assign value to a StringDictionary by using indexer

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";

}

 </source>


Get all keys from a StringDictionary

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";
   
   ArrayList ^alist = gcnew ArrayList();
   alist->AddRange(strdict->Keys);
   alist->Sort();

}

 </source>


Get Enumerator from StringCollection

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
   strcol->AddRange(tmpstr);
   StringEnumerator ^strenum = strcol->GetEnumerator();
   while ( strenum->MoveNext())
   {
       Console::WriteLine(strenum->Current);
   }

}

 </source>


Get value from StringDictionary by key

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";
   
   ArrayList ^alist = gcnew ArrayList();
   alist->AddRange(strdict->Keys);
   alist->Sort();
   
   for (int i = 0; i < alist->Count; i++)
   {
       Console::WriteLine("{0,10}:\t{1}", alist[i], 
           strdict[(String^)alist[i]]);
   }

}

 </source>


Insert string to a StringCollection at specific position

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
   strcol->AddRange(tmpstr);
   strcol->Insert(1, "Second");

}

 </source>


Loop through a StringDictionary with for loop

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringDictionary ^strdict = gcnew StringDictionary();
   strdict->Add("D", "d");
   strdict->Add("F", "f");
   strdict["C"] = "c";
   
   ArrayList ^alist = gcnew ArrayList();
   alist->AddRange(strdict->Keys);
   alist->Sort();
   
   for (int i = 0; i < alist->Count; i++)
   {
       Console::WriteLine("{0,10}:\t{1}", alist[i], 
           strdict[(String^)alist[i]]);
   }

}

 </source>


Using for each loop with StringCollection

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   StringCollection ^strcol = gcnew StringCollection();
   array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
   strcol->AddRange(tmpstr);
   for each (String^ s in strcol)
       Console::WriteLine(s);

}

 </source>