String Class: Important Member Functions in VB.NET Part 2

In this article I will explain you about the important Member Functions in VB.NET.
  • 2296

As you learned member functions of the string class in my previous article the remaining some function we discuss in this article. So, If you want to insert a string into the middle of another string, you can use the Insert method. The Insert method is illustrated in below example by having a string to be inserted into an existing string at a chosen position.

Example of String.Insert

    Imports System
    Public Class MainClass
        Shared Sub Main()
            Dim river As String = "manish"
            Console.Write("The String before insertion is: ")
            Console.WriteLine(river)
            Console.Write("The String after insertion is: ")
            Console.WriteLine(river.Insert(5, " domain"))
            Console.ReadLine()
        End Sub
    End
Class

Output

string6.gif
 

The Join function joins two strings and string arrays into one body:

  • String.Join(string str, string[] strarr) joins the string arrays using str.

  • String.Join(string str, string[] strarr, integer i, integer j) joins the string arrays using str, starting from the integer array element and continuing j number of elements after it.

The StartsWith method is similar to the EndsWith method, except it verifies that a substring exists at the beginning of the parent string. Example of StartWith is given below:

Example of String.StartsWith

    Imports System
    Public Class MainClass
        Shared Sub Main()
            Dim strings As String()
            Dim i As Integer
            Dim
quotes As Char = ChrW(34)
            strings =
New String() {"manish", "tewatia"}
            Console.WriteLine(" Test every string to see if it starts with 'st'")
            For i = 0 To strings.GetUpperBound(0)
                If strings(i).StartsWith("ma") Then
                    Console.WriteLine(quotes & strings(i) & quotes & _
                    " starts with " & quotes & "ma" & quotes)
                Else
                    Console.WriteLine(quotes & strings(i) & quotes & _
                    " Not starts with " & quotes & "ma" & quotes)
                End If
            Next
            Console.ReadLine()
        End Sub ' Main
    End Class

Output

string7.gif
 

The Split function splits a string delimited with some specified characters. It identifies the substrings that are delimited by one or more characters specified in an array, then returns these substrings in a string array. Delimiter characters are not included in the substrings. The Split function has forms like this:

  • string.Split(params char)

  • string.Split(params char separator)

The Split function returns one of these:

  • An array consisting of a single element containing the entire string if the string contains none of the characters in the separator list.

  • An array of substrings if the string is delimited by one or more of the characters specified in the separator list passed in the Split method.

  • An array of substrings in a string delimited by white space characters if those characters occur and the separator array passed is a null reference or contains no delimiter characters.

  • String.Empty (a static read-only field that represents the empty string) where two separator are adjacent or a separator is found at the beginning or end of the string.

Example of String.Split

    Imports System
    Imports System.Collections
    Public Class MainClass
        Shared Sub Main(ByVal args As String())
            Dim s1 As String = "One,Two,Three "
            Const Space As Char = " "c
            Const Comma As Char = ","c
            Dim delimiters() As Char = {Space, Comma}
            Dim output As String = ""
            Dim ctr As Integer = 0
            Dim resultArray As String() = s1.Split(delimiters)
            Dim subString As String
            For
Each subString In resultArray
                ctr = ctr + 1
                output &= ctr.ToString()
                output &=
": "
                output &= subString
                output &=
Environment.NewLine
            Next subString
            Console.WriteLine(output)
        End Sub
    End
Class

Output

string8.gif
 

Conclusion

Hope this article would have helped you in understanding the important Member Functions in VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.