字符串函数 (Visual Basic)String Functions (Visual Basic)

下表列出了 Visual Basic 在类中提供的 Microsoft.VisualBasic.Strings 用于搜索和操作字符串的函数。The following table lists the functions that Visual Basic provides in the Microsoft.VisualBasic.Strings class to search and manipulate strings. 它们可以视为 Visual Basic 内部函数;也就是说,您不必将它们作为类的显式成员进行调用,如示例所示。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. 类中提供了其他方法,在某些情况下为互补方法 System.StringAdditional methods, and in some cases complementary methods, are available in the System.String class.

.NET Framework 方法.NET Framework method 说明Description
Asc, AscWAsc, AscW 返回一个 Integer 值,该值表示与某个字符相对应的字符代码。Returns an Integer value representing the character code corresponding to a character.
Chr, ChrWChr, ChrW 返回与指定字符代码相关联的字符。Returns the character associated with the specified character code.
Filter 返回一个从零开始的数组,该数组包含基于指定筛选条件的 String 数组的子集。Returns a zero-based array containing a subset of a String array based on specified filter criteria.
Format 返回根据格式 String 表达式中包含的指令设置格式的字符串。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 返回以 % 字符结尾的百分比格式的表达式(即乘以 100)。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 根据字符串的比较结果返回 -1、0 或 1。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.

您可以使用Option Compare语句来设置是使用不区分大小写的文本排序顺序(由系统的区域设置( Text )决定),还是通过字符的内部二进制表示形式来比较字符串( Binary )。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). 默认的文本比较方法是 BinaryThe default text comparison method is Binary.

示例: UCaseExample: UCase

本例使用 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)

示例: LTrimExample: LTrim

此示例使用 LTrim 函数去除字符串变量的前导空格,使用 RTrim 函数去除尾随空格,This example uses the LTrim function to strip leading spaces and the RTrim function to strip trailing spaces from a string variable. 并使用 Trim 函数同时去除这两种类型的空格。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)

示例: MidExample: Mid

此示例使用 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)

示例: LenExample: Len

本例使用 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)

示例: InStrExample: InStr

本例使用 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

此示例演示同时使用 Format 格式和用户定义格式格式化值的 String 函数的各种用法。This example shows various uses of the Format function to format values using both String formats and user-defined formats. 对于日期分隔符 (/)、时间分隔符 (:) 和 AM/PM 指示符(ttt),系统显示的实际格式化输出取决于代码使用的区域设置。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.

备注

对于使用 24 小时制的区域设置,AM/PM 指示符(ttt)不显示任何内容。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