String in VBnet
String in VBnet
VB.NET provides a rich set of functionality for working with strings, both in terms of "native .NET" methods as well as the
functions found in the Microsoft.VisualBasic namespace, which will look familiar to classic VB (pre-.NET) programmers. This
article presents both the native .NET methods and the MS.VB namespace functions for string-handling. Where equivalent
functionality exists between the two methodologies, the description, syntax, and examples are presented side-by-side for easy
comparison.
Getting a String's Length
Microsoft.VisualBasic
Namespace Function
Function / Property
Name:
Description:
Syntax:
Example:
Len
Length
Len(string)
String.Length
Syntax:
VB.NET
Property
Mid
VB.NET
Method
Substring
String.Substring(start, length)
start
Part
string
Description
Required. String expression from which
characters are returned.
start
Example:
Mid(strTest, 3, 4) = "xxxx"
' strTest now contains "Vixxxx Basic"
Microsoft.VisualBasic
Namespace Function
Function / Method
Name:
VB.NET
Method
Left
(NOTE: The Left function must be qualified with
the Microsoft.VisualBasic namespace
because Left is also a property in the
Windows.Forms.Form namespace.)
Description:
Syntax:
Left(string, length)
(use Substring)
Description
Required. String expression from which
the leftmost characters are returned.
strSubstr = _
Microsoft.VisualBasic.Left _
("Visual Basic", 3)
' strSubstr now contains "Vis"
Microsoft.VisualBasic
Namespace Function
Function / Method
Name:
VB.NET
Method
Right
(NOTE: The Right function must be qualified
with the Microsoft.VisualBasic namespace
because Right is also a property in the
Windows.Forms.Form namespace.)
Description:
Syntax:
Right(string, length)
Description
Required. String expression from which
the rightmost characters are returned.
(use Substring)
string is returned.
Example:
strSubstr = _
Microsoft.VisualBasic.Right _
("Visual Basic", 3)
' strSubstr now contains "sic"
It is also legal syntax to omit the ".Chars" part. If omitted, the Chars method will be assumed, as in the example below:
Dim chrTheChar As Char
Dim strTest As String = "Visual Basic"
chrTheChar = strTest(2)
' chrTheChar now contains "s"
Testing the Beginning and Ending Parts of a String with StartsWith and EndsWith
If you want to test whether or not a string begins with a certain combination of characters, you can use the StartsWith method,
which returns a Boolean True or False value indicating whether or not the string being tested starts with the characters given as
the argument. Therefore:
If strTest.StartsWith("Vis") Then ...
is equivalent to:
If strTest.Substring(0, 3) = "Vis" Then ...
or:
If Microsoft.VisualBasic.Left(strTest, 3) = "Vis" Then ...
If you want to test whether or not a string ends with a certain combination of characters, you can use the EndsWith method,
which returns a Boolean True or False value indicating whether or not the string being tested ends with the characters given as
the argument. Therefore:
If strTest.EndsWith("ic") Then ...
is equivalent to:
If strTest.Substring(10, 2) = "ic" Then ...
or:
If Microsoft.VisualBasic.Right(strTest, 2) = "ic" Then ...
VB.NET
Method
Instr
IndexOf
Syntax:
InStr
([start,] string1, string2 [, compare])
String.IndexOf(string [, startindex])
string
startindex
Description
start
string1
string2
compare
Function / Method
Name:
Microsoft.VisualBasic
Namespace Function
VB.NET
Method
InstrRev
LastIndexOf
Description:
Syntax:
InStrRev
(string1, string2[, start, [,
compare]])
String.LastIndexOf(string [, startindex])
string
startindex
Part
Description
string1
string2
start
compare
is equivalent to:
If strTest.IndexOf("asi") <> -1 Then ...
or:
If Instr(strTest, "asi") > 0 Then ...
Syntax:
Replace
VB.NET
Method
Replace
String.Replace(oldstring, newstring)
oldstring
newstring
Description
Required. String expression
containing substring to replace.
Required. Substring being
searched for.
replacewith
Required. Replacement
substring.
start
count
compare
strNewDate = _
Replace("08/31/2001", "/", "-")
' strNewDate now contains "08-31-2001"
strNewDate = _
"08/31/2001".Replace("/", "-")
' strNewDate now contains "08-31-2001"
Casing Strings
Microsoft.VisualBasic
Namespace Function
Function / Method
Name:
Description:
VB.NET
Method
UCase
ToUpper
Syntax:
UCase(string)
String.ToUpper
Example:
Function / Method
Name:
Description:
Microsoft.VisualBasic
Namespace Function
VB.NET
Method
LCase
ToLower
Syntax:
LCase(string)
String.ToLower
Example:
Trimming Strings
Microsoft.VisualBasic
Namespace Function
VB.NET
Method
Function / Method
Name:
Description:
Syntax:
LTrim(string)
String.TrimStart
Example:
LTrim
TrimStart
"
Microsoft.VisualBasic
Namespace Function
VB.NET
Method
RTrim
TrimEnd
Function / Method
Name:
Description:
Syntax:
RTrim(string)
String.TrimEnd
Example:
Function / Method
Name:
Description:
Microsoft.VisualBasic
Namespace Function
VB.NET
Method
Trim
Trim
Syntax:
Trim(string)
String.Trim
Example:
Concat
Description:
Concatenates two or more strings together. This can be used as an alternative to the + or & operators.
Syntax:
Example:
Method Name:
Insert
Description:
Syntax:
String.Insert(startindex, value)
Example:
startindex
value
Method Name:
Remove
Description:
Syntax:
String.Remove(startindex [, count])
Examples:
startindex
count
Optional. The number of characters to delete. If omitted, all characters from startindex
to the end of the string will be deleted.
Method Name:
Description:
Padding Strings
PadLeft
Returns a string that is right-aligned and padded on the left with spaces (or other specified character)
so that the length of the string is the specified width.
Syntax:
String.PadLeft(totalWidth [, paddingChar])
totalWidth
paddingChar
Optional. The character to pad the string with. If omitted, a blank space will be used.
Example:
Method Name:
Description:
PadRight
Returns a string that is left-aligned and padded on the right with spaces (or other specified character)
so that the length of the string is the specified width.
Syntax:
String.PadRight(totalWidth [, paddingChar])
totalWidth
paddingChar
Example:
Optional. The character to pad the string with. If omitted, a blank space will be used.
String Object
(NOTE: This can be used as an equivalent to the String function found in pre-.NET versions of Visual Basic)
Description:
Can be used to return a string containing a repeating character string of the length specified.
Syntax:
count
Examples:
StrReverse
Description:
Syntax:
StrReverse (string)
Examples:
Function Name:
Space
Description:
Syntax:
Space(number)
Where number is the number of blank spaces desired.
Examples:
strTest = Space(5)
' strTest now contains "
"
Function Name:
Asc
Description:
Returns an Integer representing the ASCII character code corresponding to the first letter in a string.
(For an ASCII character reference, click here.)
Syntax:
Asc(string)
Examples:
intCode =
' intCode
intCode =
' intCode
Function Name:
Asc("*")
now has the value 42
Asc("ABC")
now has the value 65
Chr
Description:
Returns a string containing the character associated with the specified ASCII character code.
(For an ASCII character reference, click here.)
Syntax:
Chr(charcode)
Where charcode is a number from 0 to 255 that identifies the character.
Examples:
strChar = Chr(65)
' strChar now contains "A"