Types of String Manipulation Methods in Visual Basic
There are several different ways to analyze and manipulate your strings. Some of the methods are a part of the Visual Basic language, and others are inherent in the String
class.
Visual Basic Language and the .NET Framework
Visual Basic methods are used as inherent functions of the language. They may be used without qualification in your code. The following example shows typical use of a Visual Basic string-manipulation command:
Dim aString As String = "SomeString"
Dim bString As String
' Assign "meS" to bString.
bString = Mid(aString, 3, 3)
In this example, the Mid
function performs a direct operation on aString
and assigns the value to bString
.
For a list of Visual Basic string manipulation methods, see String Manipulation Summary.
Shared Methods and Instance Methods
You can also manipulate strings with the methods of the String
class. There are two types of methods in String
: shared methods and instance methods.
Shared Methods
A shared method is a method that stems from the String
class itself and does not require an instance of that class to work. These methods can be qualified with the name of the class (String
) rather than with an instance of the String
class. For example:
Dim equal As Boolean = (String.Compare("Hello", "Goodbye") = 0)
In the preceding example, the String.Compare method is a shared method that accepts two instances of String
as arguments.
Instance Methods
Instance methods, by contrast, stem from a particular instance of String
and must be qualified with the instance name. For example:
Dim aString As String = "A String"
Dim bString As String
' Assign "String" to bString.
bString = aString.Substring(2, 6)
In this example, the String.Substring method is a method of the instance of String
(that is, aString
). It performs an operation on aString
and assigns that value to bString
.
For more information, see the documentation for the String class.