字串函式 (Visual Basic)

下表列出 Visual Basic 在 Microsoft.VisualBasic.Strings 類別中提供的函式,用於搜尋和操作字串。 這些函式可視為 Visual Basic 內建函式;也就是說,您無須以類別的明確成員呼叫,如下列範例所示。 其他方法和互補方法 (在某些情況下) 都會在 System.String 類別中提供。

.NET Framework 方法 描述
AscAscW 傳回 Integer 值,代表某字元的對應字元碼。
ChrChrW 傳回與指定的字元碼關聯的字元。
Filter 傳回以零起始的陣列,其中包含以指定篩選準則為依據的 String 陣列子集。
Format 傳回字串,其格式化方式是根據格式 String 運算式內包含的指令。
FormatCurrency 使用系統控制台中定義的貨幣符號,傳回格式化成貨幣值的運算式。
FormatDateTime 傳回表示日期/時間值的字串運算式。
FormatNumber 傳回格式化成數字的運算式。
FormatPercent 傳回格式化為百分比 (也就是乘以 100) 且尾端包含 % 字元的運算式。
InStr 傳回整數,指定一個字串在另一個字串內第一次出現的起始位置。
InStrRev 傳回某個字串在另一個字串中第一次出現的位置,從字串的右邊開始。
Join 傳回結合包含在陣列中幾個子字串所建立的字串。
LCase 傳回已轉換成小寫的字串或字元。
Left 傳回字串,其中包含從字串的左邊開始的指定數目的字元。
Len 傳回整數,其中包含字串中的字元數目。
LSet 傳回靠左對齊的字串,其中包含調整為指定之長度的指定字串。
LTrim 傳回字串,其中包含不含前置空格的指定字串副本。
Mid 傳回字串,其中包含字串中指定的字元數目。
Replace 傳回字串,其中的指定之子字串已經被另一個子字串取代了指定的次數。
Right 傳回字串,其中包含從字串的右邊開始的指定數目的字元。
RSet 傳回靠右對齊的字串,其中包含調整為指定之長度的指定字串。
RTrim 傳回字串,其中包含不含前置空格的指定字串副本。
Space 傳回字串,此字串是由指定之空格數所組成。
Split 傳回以零起始的一維陣列,其中包含指定之子字串數目。
StrComp 根據字串比較的結果傳回 -1、0 或 1。
StrConv 傳回依照指定方式轉換的字串。
StrDup 傳回由重複指定次數的指定字元所組成的字串或物件。
StrReverse 傳回字串,其中的指定之字串的字元順序會顛倒。
Trim 傳回字串,其中包含不含前置或後置空格的指定字串副本。
UCase 傳回包含已轉換成大寫之指定字串的字串或字元。

您可使用 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

此範例將示範 Format 函式的各種使用方式,以透過 String 格式及使用者定義的格式,將值格式化。 對於日期分隔符號 (/)、時間分隔符號 (:) 和 AM/PM 指示器 (ttt) 而言,系統顯示的實際格式化輸出需視程式碼使用的地區設定而定。 當時間和日期顯示在開發環境內時,會使用程式碼地區設定的簡短時間格式和簡短日期格式。

注意

若為使用 24 小時制的地區設定,AM/PM 指示器 (ttt) 不會顯示任何內容。

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%")

另請參閱