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

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

Here are some System.String class member functions:

The ToUpper function converts a string to uppercase.: String.ToUpper().

The ToLower function converts a string to lowercase: String.ToLower().

The Concat function concatenates strings together:

  • String.Concat(String[]) \\concatenates the string array

  • String.Concat(String, String) \\concatenates the two strings

  • String.Concat(String, String, String) \\concatenates the three strings

  • String.Concat(String, String, String, String) \\concatenates the four strings

Below shows an example of the String.Concat methods.

Example of Writing concatenated strings to the console

    Imports System
    Imports System.Collections
    Public Class MainClass
        Shared Sub Main()
            Dim a As String = "manish"
            Dim b As String = "tewatia"
            ' concatenation method
            Dim c As String = String.Concat(a, b)
            Console.WriteLine("s3 concatenated from a and b: {0}", c)
            ' use the overloaded operator
            Dim d As String = a & b
            Console.WriteLine("s4 concatenated from a & b: {0}", d)
            Console.ReadLine()
        End Sub
    End
Class

Output

string1.gif
 

In next example the Copy function returns a new string.

 Example of String.Copy

    Imports System
    Imports System.Collections
    Public Class MainClass
        Shared Sub Main()
            Dim a As String = "manish"
            Dim b As String = "tewatia"
            ' the String copy method
            Dim c As String = String.Copy(a)
            Console.WriteLine("a copied from b: {0}", a)
            ' use the overloaded operator
            Dim d As String = b
            Console.WriteLine("a = b: {0}", b)
            Console.ReadLine()
        End Sub
    End
Class

Output

string2.gif
 

In the next example the CopyTo function copies a part of one string to another string.

Example of String.CopyTo

    Imports
System
    Public Class MainClass
        Shared Sub Main()
            Dim string1 As String
            Dim
characterArray As Char()
            Dim i As Integer
            Dim
quotes As Char = ChrW(34)
            string1 =
"manish tewatia"
            characterArray = New Char(6) {}
            Console.WriteLine("string1: " & quotes & string1)
            Console.WriteLine(" copy characters from string1 into characterArray ")
            string1.CopyTo(0, characterArray, 0, 6)
            Console.WriteLine("The character array is: ")
            For i = 0 To characterArray.GetUpperBound(0)
                Console.WriteLine(characterArray(i))
            Next
            Console.ReadLine()
        End Sub ' Main
    End Class

Output

string3.gif
 

The next example expail the EndsWith function

Example of  String.EndsWith

    Imports System
    Public Class MainClass
        Shared Sub Main()
            Dim river As String = "manish"
            Console.WriteLine(river.EndsWith("h"))
            Console.WriteLine(river.EndsWith("i"))
            Console.ReadLine()
        End Sub
    End
Class

Output

string4.gif
 

The Format static function helps in formating strings containing numbers, dates, strings, and much more. Using the Format method you can pass in objects of different types and have them neatly inserted into your string. You can also handle column alignment and column width in the string. Below is a example of Format method.

Example of String.Format

    Imports
System
    Public Class MainClass
        Shared Sub Main(ByVal args As String())
            Dim amount, principal As Decimal ' dollar amounts
            Dim rate As Double ' interest rate
            Dim year As Integer ' year counter
            Dim output As String ' amount after each year
            principal = 3000
            rate = 0.09
            output =
"Year" & vbTab & "Amount deposit" & vbCrLf
            ' calculate amount after each year
            For year = 1 To 5
                amount = principal * (1 + rate) ^ year
                output &= year & vbTab & _
                String.Format("{0:C}", amount) & vbCrLf
            Next
            ' display output
            Console.WriteLine("Compound Interest" & output)
            Console.ReadLine()
        End Sub
    End
Class

Output

string5.gif
 

Conclusion

Hope this article would have helped you in understanding the important Member Functions in VB.NET. Reaming function I will describe in next article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.