你可以使用
Option Compare
语句来设置是按照系统区域设置 (
Text
) 确定的还是字符的内部二进制表示 (
Binary
) 确定的不区分大小写的文本排序顺序来比较字符串。 默认的文本比较方法是
Binary
。
示例:UCase
本例使用
UCase
函数返回字符串的大写版本。
' String to convert.
Dim lowerCase As String = "Hello World 1234"
' Returns "HELLO WORLD 1234".
Dim upperCase As String = UCase(lowerCase)
示例:LTrim
此示例使用
LTrim
函数去除字符串变量的前导空格,使用
RTrim
函数去除尾随空格, 并使用
Trim
函数同时去除这两种类型的空格。
' 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)
示例:Mid
此示例使用
Mid
函数返回某一字符串中的指定数量的字符。
' 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)
示例:Len
本例使用
Len
返回字符串中的字符数。
' Initializes variable.
Dim testString As String = "Hello World"
' Returns 11.
Dim testLen As Integer = Len(testString)
示例:InStr
本例使用
InStr
函数返回一个字符串在另一个字符串中的第一个匹配项的位置。
' 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")
此示例演示同时使用
Format
格式和用户定义格式格式化值的
String
函数的各种用法。 对于日期分隔符 (
/
)、时间分隔符 (
:
) 和 AM/PM 指示符(
t
和
tt
),系统显示的实际格式化输出取决于代码使用的区域设置。 当在开发环境中显示时间和日期时,使用代码区域设置的短时间格式和短日期格式。
对于使用 24 小时制的区域设置,AM/PM 指示符(
t
和
tt
)不显示任何内容。
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%")
Visual Basic 运行库成员
字符串操作摘要
System.String 类方法