StringCollection in VB.NET

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. In this article, we will discuss how to take advantages of its methods and properties to manage a collection of strings.
  • 3450
 

StringCollection

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. In this article, we will discuss how to take advantages of its methods and properties to manage a collection of strings.

StringCollection class defined in the System.Collections.Specialized namespace represents a collection of strings and provides functionality to manage the collection.

Creating StringCollection

The following code snippet creates an object of StringCollection class using the default constructor.

Dim authorNames As New StringCollection

Adding Strings

StringCollection class provides following three methods to add strings to a string collection.

  • Add
  • AddRange
  • Insert

Add method is used to add string to a StringCollection at the end of the collection. The following code snippet adds strings to a StringCollection.

' Add string using Add method

authorNames.Add("Mahesh Chand")

authorNames.Add("Mike Gold")

authorNames.Add("Praveen Kumar")

authorNames.Add("Raj Beniwal")

 

AddRange method is used to add an array of strings to a StringCollection at the end of the collection. The following code snippet adds an array of strings to a StringCollection.

' Add an array of string using AddRange           

Dim names As String() = New String() {"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"}

authorNames.AddRange(names)

 

Insert method is used to insert a string at the specified location of a StringCollection. The following code snippet inserts a string at the 5th position in a StringCollection. You will get out of bounds error message if the StringCollection does not have 5 items in it.

authorNames.Insert(5, "New Author")

Accessing Strings

The For Each statement in VB.NET is used to iterate through a collection of objects such as integer or string. 

The following code snippet creates an array of strings, adds strings to a StringCollection and later uses foreach statement to loop through the collection and display on the system console.

For Each name As String In authorNames

            Console.WriteLine(name)

Next name

 

Removing Strings

StringCollection class provides following three methods to remove strings to a string collection.

  • Clear
  • Remove
  • RemoveAt

Clear method removes all items from a StringCollection. The following code snippet removes all items from a StringCollection.

authorNames.Clear()

 

Remove method removes the first occurrence of a given string from the string collection. The following code snippet removes a string from a StringCollection.

authorNames.Remove("Mike Gold")

 

RemoveAt method removes a string specified at the given location from the string collection. The following code snippet removes a string at the specified index from a StringCollection.

authorNames.RemoveAt(5)

Find String

IndexOf method searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. The following code snippet finds the position of a string in a string collection.

Dim authorLocation As Integer = authorNames.IndexOf("Mike Gold")

Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString())

Contains method returns true if a string is found in a StringCollection. The following code snippet checks if a string is found in the collection and returns the position in a string collection.

If authorNames.Contains("Mike Gold") Then

    Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"))

End If

Copy Strings

CopyTo method of StringCollection is used to copy items from a StringCollection to an array. The CopyTo method takes two arguments. First is the name of the StringCollection and second is the starting position in the StringCollection. The following code snippet copies all items from authorNames StringCollection to an array.

' Copy Collection to new Array

Dim newAuthorList(authorNames.Count) As String

authorNames.CopyTo(newAuthorList, 0)

 

For Each name As String In newAuthorList

    Console.WriteLine(name)

Next name

Count Strings

Count property returns total number of items in in a StringCollection. The following code snippet returns number of items in authorNames collection.

Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString())

 

Getting Items


ArrayCollection is a collection. That means, you can access its items by using an index. The following code snippet looks for the position of a string and accesses it using Item property.

Dim counter As Integer = authorNames.IndexOf("Mike Gold")

Dim authorName As String = authorNames(counter)

 

Complete Code


Here is the listing of complete code we have discussed in this article.

Imports System

Imports System.Collections

Imports System.Collections.Specialized

 

Module Module1

 

    Sub Main()

 

        Dim authorNames As New StringCollection

 

        ' Add string using Add method

        authorNames.Add("Mahesh Chand")

        authorNames.Add("Mike Gold")

        authorNames.Add("Praveen Kumar")

        authorNames.Add("Raj Beniwal")

 

        ' Add an array of string using AddRange           

        Dim names As String() = New String() {"Mahesh Chand", "Mike Gold", "Praveen Kumar", "Raj Beniwal"}

        authorNames.AddRange(names)

 

        authorNames.Insert(5, "New Author")

 

        Dim authorLocation As Integer = authorNames.IndexOf("Mike Gold")

        Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString())

 

        If authorNames.Contains("Mike Gold") Then

            Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"))

        End If

 

 

        For Each name As String In authorNames

            Console.WriteLine(name)

        Next name

 

 

        ' Copy Collection to new Array

        Dim newAuthorList(authorNames.Count) As String

        authorNames.CopyTo(newAuthorList, 0)

 

        For Each name As String In newAuthorList

            Console.WriteLine(name)

        Next name

 

        Dim counter As Integer = authorNames.IndexOf("Mike Gold")

        Dim authorName As String = authorNames(counter)

 

 

        Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString())

 

        Console.ReadLine()

 

 

    End Sub

 

End Module

 

 

Summary

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. In this article, we saw how to take advantages of this class and its methods and properties to manage a collection of strings.

© 2013 dotNetheaven. All rights reserved.