संपादित करें

इसके माध्यम से साझा किया गया


String Functions (Visual Basic)

The following table lists the functions that Visual Basic provides in the Microsoft.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 the System.String class.

.NET Framework method Description
Asc, AscW Returns an Integer value representing the character code corresponding to a character.
Chr, ChrW Returns the character associated with the specified character code.
Filter Returns a zero-based array containing a subset of a String array based on specified filter criteria.
Format Returns a string formatted according to instructions contained in a format String expression.
FormatCurrency Returns an expression formatted as a currency value using the currency symbol defined in the system control panel.
FormatDateTime Returns a string expression representing a date/time value.
FormatNumber Returns an expression formatted as a number.
FormatPercent Returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.
InStr Returns an integer specifying the start position of the first occurrence of one string within another.
InStrRev Returns the position of the first occurrence of one string within another, starting from the right side of the string.
Join Returns a string created by joining a number of substrings contained in an array.
LCase Returns a string or character converted to lowercase.
Left Returns a string containing a specified number of characters from the left side of a string.
Len Returns an integer that contains the number of characters in a string.
LSet Returns a left-aligned string containing the specified string adjusted to the specified length.
LTrim Returns a string containing a copy of a specified string with no leading spaces.
Mid Returns a string containing a specified number of characters from a string.
Replace Returns a string in which a specified substring has been replaced with another substring a specified number of times.
Right Returns a string containing a specified number of characters from the right side of a string.
RSet Returns a right-aligned string containing the specified string adjusted to the specified length.
RTrim Returns a string containing a copy of a specified string with no trailing spaces.
Space Returns a string consisting of the specified number of spaces.
Split Returns a zero-based, one-dimensional array containing a specified number of substrings.
StrComp Returns -1, 0, or 1, based on the result of a string comparison.
StrConv Returns a string converted as specified.
StrDup Returns a string or object consisting of the specified character repeated the specified number of times.
StrReverse Returns a string in which the character order of a specified string is reversed.
Trim Returns a string containing a copy of a specified string with no leading or trailing spaces.
UCase Returns a string or character containing the specified string converted to uppercase.

You can use the Option 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 is Binary.

Example: UCase

This example uses the UCase 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 the LTrim function to strip leading spaces and the RTrim function to strip trailing spaces from a string variable. It uses the Trim 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 the Mid 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 uses Len 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 the InStr 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 the Format function to format values using both String formats and user-defined formats. For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), 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 and tt) 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