February 23, 2019
Hot Topics:

Discovering Visual Basic .NET: Using Functions and Arguments

  • December 30, 2004
  • By Bill Hatfield
  • Send Email »
  • More Articles »


With the Space function, you can easily create a string with lots of spaces in it. Just send a number indicating how many spaces you want, and Space returns a string of that length, filled with spaces:


This function returns "               " (15 spaces).

Left, Mid, Right

Left, Mid, and Right are very useful. They enable you to tear apart a string into smaller strings.

Left takes two arguments: a string and a number. It returns a string that consists of the left-most characters of the string sent:

Left("Dog bones",3)

This function returns "Dog".


This function returns "Rubb".

Right works exactly the same way, but it takes the characters from the right side of the string:

Right("Dog bones",5)

This function returns "bones".

Mid takes characters from anywhere within a string. The first argument is the string you want to work with. The second argument determines where in the string to begin, and the third argument determines how many characters to use. Mid returns the string that results:

Mid("Quick brown fox",7, 5)

This function returns "brown".

It starts with the seventh character ("b") and takes five characters total (including the seventh).


InStr is a really handy function. It searches for a shorter string within a long string. If it finds the shorter string, InStr returns the position within the larger string where the shorter string can be found.

In other words, InStr returns the character position at which it finds the second argument within the first argument.

Here's an example:

Imports System
Imports Microsoft.VisualBasic

Module EggsHam
  Public Sub Main()
    Dim BigString, FindString As String
    Dim Position As Integer
    BigString = "I do not like green eggs and ham."
    FindString = "eggs"
    Position = InStr(BigString, FindString)
    Console.WriteLine("This string: " & FindString)
    Console.WriteLine("Appears inside: " & BigString)
    Console.WriteLine("At position " & Position)
  End Sub
End Module

Position holds the value 21, because "eggs" starts at the 21st position in BigString.


Replace goes a step further than InStr. It looks for a small string inside a larger one, and when the small string is found, it is replaced with another specified string.

Here's an example:

Imports System
Imports Microsoft.VisualBasic

Module MaryLamb
  Public Sub Main()
    Dim BigString as string
    BigString = "Mary had a little lamb"
    Console.WriteLine(Replace(BigString, "little", "big"))
  End Sub
End Module

This program displays the following line:

Mary had a big lamb


Format enables you to take a date, time, or number and format it the way you want it to look. You send two arguments. The first can be a date, a time, or a number. Or, you can use a variable holding a date, time, or number. The second argument can hold many different values depending on what you're trying to format and how you want it to look.

Here's an example:

Imports System
Imports Microsoft.VisualBasic

Module VariousFormats
  Public Sub Main()
    Console.WriteLine(Format(Now,"MMM dd, yyyy"))
  End Sub
End Module

Here's the result this program produces:

67.50 %

12/21/2004 6:20 PM
6:20 PM

Dec 21, 2004

As this example shows, you can use many letters for the second argument, and each letter has a predefined meaning. Table 1 lists common examples of the letters you can use for this argument.

Table 1. Common Format arguments.

Letter What It Means
p Percentage
n Standard
c Currency
g General date/time
d Short date
t Short time

You also can enter your own format using certain letters in combination, as the last Format line in the example shows. For more details and options, see the .NET Framework documentation.

Page 4 of 5

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date