This next example, taken from the book "Beginning ASP.NET 3.5 in VB 2008," perfectly shows the manipulation techniques found in VB.NET.

NOTE: In Visual Basic, we can write comments by adding a single quote ("'") to the beginning of the line.

Dim MyString As String = "This is a test string     "
MyString = MyString.Trim()
'the Trim function removes the outer spaces.
'MyString now has the value "This is a test string"
MyString = MyString.Substring(0,4)
'the Substring function takes a smaller string out of the whole string.
'MyString has the value "This"
MyString = MyString.ToUpper()
'the ToUpper function capitalizes all of the letters in the string.
'MyString has the value "THIS"
MyString = MyString.Replace("IS","AT")
'the Replace function replaces all instances of one string with another.
'MyString has the value "THAT"
Dim Length As Integer = MyString.Length
'the Length function returns the number of characters in the string.
'Length has the value 4.

NOTE: The Substring function has two arguments. The first being the first index of the substring, and the second beiong the length.

NOTE: The Replace function has two arguments. The first being the value to replace, and the second being the value to replace it with.

Another function worth mentioning is the IndexOf(arg). This function accepts one argument and returns the first position of that argument in the string. Keep in mind that the index counts the first position as "0", similar to the array.

Functions can also be used one directly after another on the same object. For example:

Dim strExample As String = "hello     "
strExample.Trim().Substring(0,2).ToUpper().Replace("HE","SHE")

If you followed that example along, the final product is "SHE." Oddly enough, you can also use these functions on a string before even defining it to a variable. For example:

Dim strExample As String = "hello".Replace("he","she")

This will set the variable strExample to "shello"

Here are some other popular functions:

  • .Length() - Returns the number of characters in the string as an integer.
  • .ToUpper() - Returns the string in all uppercase.
  • .ToLower() - Returns the string in all lowercase.
  • .TrimStart() - Returns the string after trimming all of the white space at the start of the string.
  • .TrimEnd() - Returns the string after trimming all of the white space at the end of the string.
  • .Trim() - Returns the string after trimming all of the white space at both the start and end of the string
  • .PadLeft(int StringSize, String to use for padding) - Returns a string after adding padding to the left of your string to force it to a certain length. For example:
    Dim hiString As String = "hi".PadLeft(8, "#")

    hiString will be set with a value of "######hi"

  • .PadRight(int StringSize, String to use for padding) - Returns a string after adding padding to the right of your string to force it to a certain length.
  • .Insert(int Index, String to insert) - Inserts a string into an existing string at the set index.
  • .Remove(int index to start, int number of spaces) - Removes the substring from the specified start index to the specified number of spaces thereafter.
  • .StartsWith(String to check) - Checks if the specified string to check is at the beginning. Returns a Boolean (true/false).
  • .EndsWith(String to check) - Checks if the specified string to check is at the end. Returns a Boolean (true/false).
  • .LastIndexOf - Returns the index (integer) of what you are looking for when it last appears.
  • .Split(String to split by) - Splits a string up by the argument, and puts the substrings into an array. For example:
    Dim ArrayName() as String = "hi, how is it going?".Split("i")

    ArrayName() now has these values:

    • ArrayName(0) = "h"
    • ArrayName(1) = ", how "
    • ArrayName(2) = "s "
    • ArrayName(3) = "t go"
    • ArrayName(4) = "ng?"
  • .Join(String to seperate substrings with) - The argument in this function is optional. This function does the opposite of .Split(). For example, we can use the following code to reverse the above example:
    ArrayName.Join("i")

Next, we will discuss the DateTime and TimeSpan objects in Visual Basic.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!