共用方式為


字串函式 (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 式,從字串傳回指定的字元數。

' 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指標 (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%")

另請參閱