One of the most used types in VB.NET is the String
type.
This article will look at
the String
type and what it actually can do.
Substring
A substring is a string within a string. Any string that has more than 0 character can have a substring within it.
The following is an example of this.
Jim moved house today
Within this sample, there are multiple different substrings (in fact, about 5.84258701838598 x 1027 substrings) in this piece of text. Some examples include "Jim moved", "Jim", "house", "house today" and "Jim mo". A substring can of any length from 0 to the length of the string.
In VB.NET, the Substring
function is called from a String
type as shown below:
Dim myString As String = "Hello world" myString = myString.Substring(3, 2) MsgBox(myString)
This will display a message box with the content "lo " (as VB.NET starts enumerating characters at 0, every character is therefore (n-1)th position in VB.NET counting, so H is the 0th character).
Trim
The trim method is used to remove any characters from the start and end of a string. For instance, if the Trim
function with the parameter "-"
is applied to "---Jack is a dull boy---"
, the result will look like: "Jack is a dull boy"
. This is a particularly useful way of removing white space at the start of a string.
Trim can also accept an array of values to remove.
Dim myString As String = "***+My name is Jack+++" Dim trims[] As Char = {"*", "+"} myString = myString.Trim(trims) MsgBox(myString)
The end result will be "My name is Jack".
TrimStart
There is also a TrimStart
function available on the String
type. These work on either side of
the string rather than both at the same time.
Dim myString As String = "***+My name is Jack+++" Dim trims[] As Char = {"*", "+"} myString = myString.TrimStart(trims) MsgBox(myString)
This example will output "My name is Jack+++".
TrimEnd
The opposite to TrimStart
is obviously TrimEnd
which will remove from the opposite side.
Dim myString As String = "***+My name is Jack+++" Dim trims[] As Char = {"*", "+"} myString = myString.TrimEnd(trims) MsgBox(myString)
The output from this program will be "***+My name is Jack".
Contains
The Contains
function returns a Boolean variable to state whether
a string of text contains another, that is A ⊂ B.
If this is the case it returns True
otherwise it returns False
.
The method is particularly useful when for performing a find and replace method as it can confirm the presense of a character or string within a string.
For the following string, the result will be true.
Dim myString As String = "My name is Jack" MsgBox(myString.Contains("Jack"))
Length
The Length
method is perhaps one of the most useful methods in the whole language, not just
for the String
type.
As a String
is an array of the Char
type, it can be used to obtain how many Char
s are
within it.
Dim myString As String = "My name is Jack" Dim length As Integer = myString.Length() MsgBox(length)
Concatenating strings
VB.NET has a very simple set of concatenation characters. The & and + symbols can both be used to join two or more strings together. It takes order of precedence when returning the concatenated string.
Dim myString1 As String = "My name is Jack" Dim myString1 As String = " and I am 21 years old." MsgBox(myString1 & myString2)
Dim myString1 As String = "My name is Jack" Dim myString2 As String = " and I am 21 years old." MsgBox(myString1 + myString2)
There is another option for this however. The third option for concatenating two strings uses a method from
within the string type. It is simply called Concat
Dim myString1 As String = "My name is Jack" Dim myString2 As String = " and I am 21 years old." MsgBox(myString1.Concat(myString2))
Concat
method is actually a function with a return type of String
. It
does not do anything to the original string.
Changing the case of a string
Changing the case of a string can be useful not only for the sake of formatting a string but also to compare two strings for equality.
Lowercase
The LCase
function is given a string as a parameter and convert it to the lowercase equivelant.
Dim myString As String = "My name is Jack" myString = LCase(myString)
Uppercase
The UCase
function is the same as the lowercase version except it transforms the text to the uppercase version.
Dim myString As String = "My name is Jack" myString = UCase(myString)
Comparing strings using case
Strings can be compared for absolute equality or they can be compared for their literal equality. For absolute equality: "Hello" ≠ "hello" but for literal equality: "Hello" = "hello". If trying to compare by literal equality the case must be normalised. The following sample shows how to create a function that does this.
Public Function literalEquality(ByVal string1 As String, ByVal string2 As String) As Boolean Return LCase(string1) = LCase(string2) End Function
By applying the parameters "Hello" and "hello" will return true.