Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

String Functions (Visual Basic)

  • 2021-09-15
Feedback

In this article

The following table lists the functions that Visual Basic provides in theMicrosoft.VisualBasic.Strings class to search and manipulate strings. They can be regarded as Visual Basic intrinsic functions; that is, you do not have to call them as explicit members of a class, as the examples show. Additional methods, and in some cases complementary methods, are available in theSystem.String class.

.NET Framework methodDescription
Asc,AscWReturns anInteger value representing the character code corresponding to a character.
Chr,ChrWReturns the character associated with the specified character code.
FilterReturns a zero-based array containing a subset of aString array based on specified filter criteria.
FormatReturns a string formatted according to instructions contained in a formatString expression.
FormatCurrencyReturns an expression formatted as a currency value using the currency symbol defined in the system control panel.
FormatDateTimeReturns a string expression representing a date/time value.
FormatNumberReturns an expression formatted as a number.
FormatPercentReturns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.
InStrReturns an integer specifying the start position of the first occurrence of one string within another.
InStrRevReturns the position of the first occurrence of one string within another, starting from the right side of the string.
JoinReturns a string created by joining a number of substrings contained in an array.
LCaseReturns a string or character converted to lowercase.
LeftReturns a string containing a specified number of characters from the left side of a string.
LenReturns an integer that contains the number of characters in a string.
LSetReturns a left-aligned string containing the specified string adjusted to the specified length.
LTrimReturns a string containing a copy of a specified string with no leading spaces.
MidReturns a string containing a specified number of characters from a string.
ReplaceReturns a string in which a specified substring has been replaced with another substring a specified number of times.
RightReturns a string containing a specified number of characters from the right side of a string.
RSetReturns a right-aligned string containing the specified string adjusted to the specified length.
RTrimReturns a string containing a copy of a specified string with no trailing spaces.
SpaceReturns a string consisting of the specified number of spaces.
SplitReturns a zero-based, one-dimensional array containing a specified number of substrings.
StrCompReturns -1, 0, or 1, based on the result of a string comparison.
StrConvReturns a string converted as specified.
StrDupReturns a string or object consisting of the specified character repeated the specified number of times.
StrReverseReturns a string in which the character order of a specified string is reversed.
TrimReturns a string containing a copy of a specified string with no leading or trailing spaces.
UCaseReturns a string or character containing the specified string converted to uppercase.

You can use theOption Compare statement to set whether strings are compared using a case-insensitive text sort order determined by your system's locale (Text) or by the internal binary representations of the characters (Binary). The default text comparison method isBinary.

Example: UCase

This example uses theUCase function to return an uppercase version of a string.

' String to convert.Dim lowerCase As String = "Hello World 1234"' Returns "HELLO WORLD 1234".Dim upperCase As String = UCase(lowerCase)

Example: LTrim

This example uses theLTrim function to strip leading spaces and theRTrim function to strip trailing spaces from a string variable. It uses theTrim function to strip both types of spaces.

' Initializes string.Dim testString As String = "  <-Trim->  "Dim trimString As String' Returns "<-Trim->  ".trimString = LTrim(testString)' Returns "  <-Trim->".trimString = RTrim(testString)' Returns "<-Trim->".trimString = LTrim(RTrim(testString))' Using the Trim function alone achieves the same result.' Returns "<-Trim->".trimString = Trim(testString)

Example: Mid

This example uses theMid function to return a specified number of characters from a string.

' Creates text string.Dim testString As String = "Mid Function Demo"' Returns "Mid".Dim firstWord As String = Mid(testString, 1, 3)' Returns "Demo".Dim lastWord As String = Mid(testString, 14, 4)' Returns "Function Demo".Dim midWords As String = Mid(testString, 5)

Example: Len

This example usesLen to return the number of characters in a string.

' Initializes variable.Dim testString As String = "Hello World"' Returns 11.Dim testLen As Integer = Len(testString)

Example: InStr

This example uses theInStr function to return the position of the first occurrence of one string within another.

' String to search in.Dim searchString As String = "XXpXXpXXPXXP"' Search for "P".Dim searchChar As String = "P"Dim testPos As Integer' A textual comparison starting at position 4. Returns 6.testPos = InStr(4, searchString, searchChar, CompareMethod.Text)' A binary comparison starting at position 1. Returns 9.testPos = InStr(1, SearchString, SearchChar, CompareMethod.Binary)' If Option Compare is not set, or set to Binary, return 9.' If Option Compare is set to Text, returns 3.testPos = InStr(searchString, searchChar)' Returns 0.testPos = InStr(1, searchString, "W")

Example: Format

This example shows various uses of theFormat function to format values using bothString formats and user-defined formats. For the date separator (/), time separator (:), and the AM/PM indicators (t andtt), the actual formatted output displayed by your system depends on the locale settings the code is using. When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.

Note

For locales that use a 24-hour clock, the AM/PM indicators (t andtt) display nothing.

Dim testDateTime As Date = #1/27/2001 5:04:23 PM#Dim testStr As String' Returns current system time in the system-defined long time format.testStr = Format(Now(), "Long Time")' Returns current system date in the system-defined long date format.testStr = Format(Now(), "Long Date")' Also returns current system date in the system-defined long date ' format, using the single letter code for the format.testStr = Format(Now(), "D")' Returns the value of testDateTime in user-defined date/time formats.' Returns "5:4:23".testStr = Format(testDateTime, "h:m:s")' Returns "05:04:23 PM".testStr = Format(testDateTime, "hh:mm:ss tt")' Returns "Saturday, Jan 27 2001".testStr = Format(testDateTime, "dddd, MMM d yyyy")' Returns "17:04:23".testStr = Format(testDateTime, "HH:mm:ss")' Returns "23".testStr = Format(23)' User-defined numeric formats.' Returns "5,459.40".testStr = Format(5459.4, "##,##0.00")' Returns "334.90".testStr = Format(334.9, "###0.00")' Returns "500.00%".testStr = Format(5, "0.00%")

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo