System.Math 類中定義了用於數學計算的函數。Math 類包括三角函數、對數函數和其他常用數學函數。下列函數是在 System 名稱空間的 Math 類中定義的函數。
注意:要使用這些函數,請通過在原始碼頂部添加以下代碼將 System.Math 名稱空間匯入到項目中:
Imports System.Math
Abs
Abs 函數返回指定數值的絕對值。
Abs 樣本:
本樣本使用 Math 類的 Abs 方法來計算一個數值的絕對值。
' Code will not compile unless it is put in a Sub or in a Function.
Dim MyNumber As Double
MyNumber = Abs(50.3) ' Returns 50.3.
MyNumber = Abs(-50.3) ' Returns 50.3.
Atan
Atan 函數返回包含角度的 Double 值。該角度的正切值是指定的數值。傳回值為正表示角度在 X 軸的逆時針方向上。傳回值為負表示順時針角度。將該傳回值乘以 180 再除以 pi (p) 就可以從弧度轉換為度。
Atan 樣本:
本樣本使用 Math 類的 Atan 方法來計算 Pi 值。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim pi As Double
pi = 4 * Atan(1) ' Calculate the value of pi.
Cos
Cos 函數以弧度表示的角度作為參數,然後返回一個 Double 值,表示指定角度的餘弦值。
Cos 樣本:
本樣本使用 Math 類的 Cos 方法返回角度的餘弦值。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyAngle, MySecant As Double
MyAngle = 1.3 ' Define angle in radians.
MySecant = 1 / Cos(MyAngle) ' Calculate secant.
Exp
Exp 函數返回一個 Double 值,包含 e(自然對數的底)的指定乘冪。使用 Pow 方法可計算其他底的乘冪。Exp 是 Log 的逆運算。
Exp 樣本:
本樣本使用 Math 類的 Exp 方法返回 e 的乘冪。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyAngle, MyHSin As Double
' Define angle in radians.
MyAngle = 1.3
' Calculate hyperbolic sine.
MyHSin = (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2
Log
Log 函數返回一個 Double 值,包含一個指定數值的對數。該方法已重載,可以返回指定數值的自然(底數 e)對數或指定數值的指定底對數。
Log 樣本:
本樣本使用 Math 類的 Log 方法返回一個數值的自然對數。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyAngle, MyLog As Double
' Define angle in radians.
MyAngle = 1.3
' Calculate inverse hyperbolic sine.
MyLog = Log(MyAngle + Sqrt(MyAngle * MyAngle + 1))
Round
Round 函數返回一個 Double 值,包含與指定值最接近的數值。其他 Round 函數可用作內部類型的方法,如 Decimal.Round 方法。
Round 樣本:
本樣本使用 Math 類的 Round 方法將一個數值四捨五入為最接近的整數。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyVar1 As Double = 2.8
Dim MyVar2 As Double
MyVar2 =Round(MyVar1) ' Returns 3.
Sign
Sign 函數返回一個指示某個數值的加號或減號的整數值。下面的表格顯示了函數及其傳回值的輸入參數:指定數傳回值
正數1
負數-1
零0
Sign 樣本:
本樣本使用 Math 類的 Sign 方法確定一個數值的加號或減號。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyVar1, MyVar2, MyVar3 As Double
Dim MySign As Integer
MyVar1 = 12
MyVar2 = -2.4
MyVar3 = 0
MySign = Sign(MyVar1) ' Returns 1.
MySign = Sign(MyVar2) ' Returns -1.
MySign = Sign(MyVar3) ' Returns 0.
Sin
Sin 函數以弧度表示的角度作為參數,然後返回一個 Double 值,指定該角度的正弦值。
Sin 樣本:
本樣本使用 Math 類的 Sin 方法返回一個角度的正弦值。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyAngle, MyCosecant As Double
MyAngle = 1.3 ' Define angle in radians.
MyCosecant = 1 / Sin(MyAngle) ' Calculate cosecant.
Sqrt
Sqrt 函數返回一個 Double 值,指定一個指定數值的平方根。
Sqrt 樣本:
本樣本使用 Math 類的 Sqrt 方法計算一個數值的平方根。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MySqr As Double
MySqr = Sqrt(4) ' Returns 2.
MySqr = Sqrt(23) ' Returns 4.79583152331272.
MySqr = Sqrt(0) ' Returns 0.
MySqr = Sqrt(-4) ' Returns NaN (not a number).
Tan
Tan 函數返回一個 Double 值,包含指定角度的正切值。Tan 函數以弧度表示的角度作為參數。當指定的角度是 NaN、NegativeInfinity 或 PositiveInfinity 時,該方法將返回 NaN。
注意:乘以 p/180 可將度數轉換為弧度數。
Tan 樣本:
本樣本使用 Math 類的 Tan 方法返回一個角度的正切值。
' Code is not compiled unless it is put in a Sub or in a Function.
Dim MyAngle, MyCotangent As Double
MyAngle = 1.3 ' Define angle in radians.
MyCotangent = 1 / Tan(MyAngle) ' Calculate cotangent.
使用類型轉換函式
將一個值從一種資料類型更改為另一種資料類型的過程稱為轉換。轉換或者是擴大或者是收縮,這取決於所涉及的類型的資料容量。公用語言運行庫支援擴大轉換和收縮轉換兩種。例如,以 32 位有符號整數形式表示的值可以轉換為 64 位元有符號整數。這是一個擴大轉換的樣本。相反的轉換(從 64 位元到 32 位)是一個收縮轉換的樣本。使用擴大轉換資訊永遠不會丟失。但是,使用收縮轉換資訊可能會丟失。
以下是 Visual Basic .NET 中定義的類型轉換函式:
CBool
CBool 函數用於將字串運算式或數值運算式轉換為 Boolean 值。當運算式計算為一個非零值時,CBool 函數將返回 True。否則,該函數返回 False。
CBool 樣本:
Dim A, B, C As Integer
Dim Check As Boolean
A = 5
B = 5
Check = CBool(A = B) ' Check is set to True.
C = 0
Check = CBool(C) ' Check is set to False.
CByte
CByte 函數將指定數值轉換為位元組。輸入參數必須是 0 到 255 之間的一個數值。否則,將出現 System.OverflowException。
CByte 樣本:
Dim MyDouble As Double
Dim MyByte As Byte
MyDouble = 125.5678
MyByte = CByte(MyDouble) ' MyByte is set to 126.
CChar
CChar 函數僅轉換指定字串的第一個字元。CChar 的輸入參數必須是資料類型的字串。無法使用 CChar 將數值轉換為字元,因為 CChar 無法接受 Numeric 資料類型。
CChar 樣本:
本樣本使用 CChar 函數將字串運算式的第一個字元轉換為 Char 類型。
Dim MyString As String
Dim MyChar As Char
MyString = "BCD" ' CChar converts only the first character of the string.
MyChar = CChar(MyString) ' MyChar is set to "B".
CDate
CDate 接受日期和時間的任何有效表示方式,然後將其轉換為 Date 值。
CDate 樣本:
本樣本使用 CDate 函數將字串轉換為 Date 值。
Dim MyDateString, MyTimeString As String
Dim MyDate, MyTime As Date
MyDateString = "February 12, 1969"
MyTimeString = "4:35:47 PM"
' ...
MyDate = CDate(MyDateString) ' Convert to Date data type.
MyTime = CDate(MyTimeString) ' Convert to Date data type.
CDbl
CDbl 函數用於將數值運算式轉換為 Double 值。對於負值,該函數的輸入參數必須在 -4.94065645841247E-324 到 -1.79769313486231E+308 之間。對於正值,該函數的輸入參數必須在 1.79769313486231E+308 到 4.94065645841247E-324 之間。
CDbl 樣本:
Dim MyDec As Decimal
Dim MyDouble As Double
MyDec = 234.456784D ' Literal type character D makes MyDec a Decimal.
MyDouble = CDbl(MyDec * 8.2D * 0.01D) ' Convert result to a Double.
CDec
CDec 函數將數值轉換為小數。
CDec 樣本:
Dim MyDouble As Double
Dim MyDecimal As Decimal
MyDouble = 10000000.0587
MyDecimal = CDec(MyDouble) ' Convert to Decimal.
CInt
CInt 函數將數值轉換為整數。
CInt 樣本:
Dim MyDouble As Double
Dim MyInt As Integer
MyDouble = 2345.5678
MyInt = CInt(MyDouble) ' MyInt is set to 2346.
CLng
CLng 函數以一個數值作為參數,然後返回一個 Long 值。
CLng 樣本:
Dim MyDbl1, MyDbl2 As Double
Dim MyLong1, MyLong2 As Long
MyDbl1 = 25427.45
MyDbl2 = 25427.55
MyLong1 = CLng(MyDbl1) ' MyLong1 contains 25427.
MyLong2 = CLng(MyDbl2) ' MyLong2 contains 25428.
CObj
CObj 函數將數值轉換為對象。
CObj 樣本:
Dim MyDouble As Double
Dim MyObject As Object
MyDouble = 2.7182818284
MyObject = CObj(MyDouble) ' Double value is pointed to by MyObject.
CShort
CShort 函數將數值轉換為 Short 值。
CShort 樣本:
Dim MyByte as Byte
Dim MyShort as Short
MyByte = 100
MyShort = CShort(MyByte) ' Convert to Short.
CSng
CSng 函數將數值轉換為 Single 值。
CSng 樣本:
Dim MyDouble1, MyDouble2 As Double
Dim MySingle1, MySingle2 As Single
MyDouble1 = 75.3421105
MyDouble2 = 75.3421567
MySingle1 = CSng(MyDouble1) ' MySingle1 is set to 75.34211.
MySingle2 = CSng(MyDouble2) ' MySingle2 is set to 75.34216.
CStr
下面的表格顯示了 CStr 函數的輸入參數和傳回值:輸入參數資料類型傳回值
Boolean包含 True 或 False 的字串
Date包含以系統的短日期格式表示的 Date 值(日期和時間)的字串
數值表示數位字串
CStr 樣本:
本樣本使用 CStr 函數將數值轉換為字串。
Dim MyDouble As Double
Dim MyString As String
MyDouble = 437.324
MyString = CStr(MyDouble) ' MyString is set to "437.324".
使用字串函數
不同的類中都定義有字串函數。這些類包括 Microsoft.VisualBasic.Strings 類和 System.String 類。
使用 Microsoft.VisualBasic.Strings 類中的字串函數
下列函數是 Microsoft.VisualBasic.Strings 類中定義的字串函數。
注意:要使用字串函數,請通過在原始碼開始處添加以下代碼將名稱空間 Microsoft.VisualBasic.Strings 匯入到項目中:
Imports Microsoft.VisualBasic.Strings
Asc 和 AscW
Asc 函數和 AscW 函數返回一個整數值,表示與指定的字元相對應的字元代碼。這兩個函數接受任何有效字元運算式或字串運算式作為參數。當字串是輸入參數時,則僅輸入字串的第一個字元。當字串不包含任何字元時,將出現 ArgumentException 錯誤。Asc 返回輸入字元的代碼資料點或字元代碼。對於單一位元組字元集 (SBCS) 值,傳回值可以是 0 到 255 之間的數字。對於雙位元組字元集 (DBCS) 值,傳回值可以是 -32768 到 32767 之間的數字。AscW 為輸入字元返回 0 到 65535 之間的 Unicode 代碼資料點。
例如:
Dim MyInt As Integer
MyInt = Asc("A") ' MyInt is set to 65.
MyInt = Asc("a") ' MyInt is set to 97.
MyInt = Asc("Apple") ' MyInt is set to 65.
Chr 和 ChrW
Chr 函數和 ChrW 函數返回與指定的字元代碼相關聯的字元。當 CharCode 超出 -32768 到 65535 的範圍時,將出現 ArgumentException 錯誤。
例如:
本樣本使用 Chr 函數返回與指定的字元代碼相關聯的字元。
Dim MyChar As Char
MyChar = Chr(65) ' Returns "A".
MyChar = Chr(97) ' Returns "a".
MyChar = Chr(62) ' Returns ">".
MyChar = Chr(37) ' Returns "%".
GetChar
GetChar 函數返回一個 Char 值,表示指定字串的指定索引中的字元。當索引小於 1 或大於指定輸入參數中最後一個字元的索引時,將出現 ArgumentException 錯誤。
例如:
本樣本顯示了如何使用 GetChar 函數從字串的指定索引中返回字元。
Dim myString As String = "ABCDE"
Dim myChar As Char
myChar = GetChar(myString, 4) ' myChar = "D"
InStr
InStr 函數返回一個整數,指定一個字串在另一個字串中首次出現的起始位置。
例如:
以下樣本使用 InStr 函數返回一個字串在另一個字串中首次出現的位置:
Dim SearchString, SearchChar As String
Dim MyPos As Integer
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
' A textual comparison starting at position 4. Returns 6.
MyPos = InStr(4, SearchString, SearchChar, CompareMethod.Text)
Join
Join 函數返回一個字串,該字串是通過串連數組中包含的子字串建立的。包含必須串連的子字串的一維數組將作為參數傳遞給 Join 函數。該函數使用 Delimiter、String 作為選擇性參數來分隔返回的字串中的子字串。當省略 Delimiter 時,將使用空格(“ ”)作為子字串之間的分隔字元。當 Delimiter 是零長度字串 ("") 時,數組中的子字串將不使用分隔字元,而是直接相連。
例如:
以下樣本顯示了如何使用 Join 函數:
Dim myItem(2) As String
Dim myShoppingList As String
myItem(0) = "Pickle"
myItem(1) = "Pineapple"
myItem(2) = "Papaya"
' Returns "Pickle, Pineapple, Papaya"
myShoppingList = Join(myItem, ", ")
LCase
LCase 函數返回已經轉換為小寫字串或字元。只有大寫字母被轉換為小寫。所有小寫字母和非字母字元均保持不變。
例如:
以下樣本使用 LCase 函數返回字串的小寫形式:
Dim UpperCase, LowerCase As String
Uppercase = "Hello WORLD 1234" ' String to convert.
Lowercase = LCase(UpperCase) ' Returns "hello world 1234".
LTrim、RTrim 和 Trim
這些函數會返回一個包含指定字串的副本的字串。在使用 LTrim 時,沒有起始空格。在使用 RTrim 時,沒有尾隨空格。在使用 Trim 時,既沒有起始空格也沒有尾隨空格。
例如:
以下樣本使用 LTrim 函數刪除字串變數中的起始空格,使用 RTrim 函數刪除字串變數中的尾隨空格,以及使用 Trim 函數刪除字串變數中的起始空格和尾隨空格:
Dim MyString, TrimString As String
MyString = " <-Trim-> " ' Initializes string.
TrimString = LTrim(MyString) ' TrimString = "<-Trim-> ".
TrimString = RTrim(MyString) ' TrimString = " <-Trim->".
TrimString = LTrim(RTrim(MyString)) ' TrimString = "<-Trim->".
' Using the Trim function alone achieves the same result.
TrimString = Trim(MyString) ' TrimString = "<-Trim->".
Replace
Replace 函數返回一個字串,其中指定的子字串按指定的次數替換為另一個子字串。Replace 函數的傳回值是一個字串,該字串在 Start 參數指定的位置開始,然後在指定字串的末尾以 Find 參數和 Replace 參數中的值所指定的替換內容結束。
例如:
本樣本示範了 Replace 函數:
Dim myString As String = "Shopping List"
Dim aString As String
' Returns "Shipping List".
aString = Replace(myString, "o", "i")
StrComp
StrComp 函數返回 –1、0 或 1。這將基於字串比較的結果。字串將從第一個字元開始英數字元排序的值進行比較。
例如:
以下樣本使用 StrComp 函數返回字串比較的結果。如果省略第三個參數,則使用選項比較語句或項目預設設定中定義的比較類型。
Dim MyStr1, MyStr2 As String
Dim MyComp As Integer
MyStr1 = "ABCD"
MyStr2 = "abcd" ' Defines variables.
' The two strings sort equally. Returns 0.
MyComp = StrComp(MyStr1, MyStr2, CompareMethod.Text)
' MyStr1 sorts after MyStr2. Returns -1.
MyComp = StrComp(MyStr1, MyStr2, CompareMethod.Binary)
' MyStr2 sorts before MyStr1. Returns 1.
MyComp = StrComp(MyStr2, MyStr1)
StrConv
StrConv 函數返回一個字串,該字串轉換為輸入參數中指定的值。StrConv 函數將轉換字串。這種轉換基於 Conversion 參數中的值。Conversion 參數中的值是 VbStrConv 枚舉的成員。
Conversion 參數的設定為:枚舉成員說明
VbStrConv.None不執行轉換
VbStrConv.LinguisticCasing- 使用語言規則而不是檔案系統(預設值)來區分大小寫
- 僅對大寫和小寫字母有效
VbStrConv.UpperCase將字串轉換為大寫字元
VbStrConv.LowerCase將字串轉換為小寫字元
VbStrConv.ProperCase將字串中每個單詞的第一個字母轉換為大寫
例如:
以下樣本將文本轉換為小寫字母:
Dim sText, sNewText As String
sText = "Hello World"
sNewText = StrConv(sText, VbStrConv.LowerCase)
Debug.WriteLine (sNewText) ' Outputs "hello world".
StrDup
StrDup 函數返回一個由指定的字元重複指定的次數而形成的字串或對象。StrDup 函數具有兩個參數:Number 參數和 Character 參數。Number 參數指定函數必須返回的字串的長度。StrDup 函數僅使用 Character 參數中的第一個字元。Character 參數可以是 Char 資料型別、String 資料類型或 Object 資料類型。
例如:
以下樣本使用 StrDup 函數返回由重複字元組成的字串:
Dim aString As String = "Wow! What a string!"
Dim aObject As New Object()
Dim myString As String
aObject = "This is a String that is contained in an Object"
myString = StrDup(5, "P") ' Returns "PPPPP"
myString = StrDup(10, aString) ' Returns "WWWWWWWWWW"
myString = StrDup(6, aObject) ' Returns "TTTTTT"
StrReverse
StrReverse 函數返回一個字串,該字串將指定字串的字元順序顛倒過來。
例如:
Dim myString As String = "ABCDEFG"
Dim revString As String
' Returns "GFEDCBA".
revString = StrReverse(myString)
UCase
UCase 函數返回一個字串或字元,包含已轉換為大寫的指定字串。只有小寫字母被轉換為大寫字母。所有大寫字母和非字母字元均保持不變。
例如:
以下樣本使用 UCase 函數返回字串的大寫形式:
Dim LowerCase, UpperCase As String
LowerCase = "Hello World 1234" ' String to convert.
UpperCase = UCase(LowerCase) ' Returns "HELLO WORLD 1234".
使用 System.String 類中的字串函數
以下是 System 名稱空間的 String 類中的字串函數。
注意:要使用字串函數,請通過在原始碼開始處添加以下代碼將 System.String 名稱空間匯入到項目中:
Imports System.String
Compare
Compare 函數比較輸入參數中的兩個字串。通過使用單詞定序來執行比較。發現不相等情況或比較完兩個字串後,比較將終止。
Compare 樣本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "testexample"
s2 = "testex"
MsgBox(Compare(s2, s1)) 'Returns -1.
MsgBox(Compare(s1, s2)) 'Returns 1.
Concat
Concat 函數將一個或多個字串相串連,然後返回串連後的字串。
Concat 樣本:
以下樣本顯示了如何使用 Concat 的重載版本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2, sa(3) As String
sa(0) = "A"
sa(1) = "B"
sa(2) = "C"
s1 = "test"
s2 = "example"
s1 = Concat(s1, s2) 'Returns testexample.
MsgBox(s1)
MsgBox(Concat(sa)) 'Returns ABC.
Copy
Copy 函數將指定字串中的值複製到另一個字串中。
Copy 樣本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "Hello World"
'Copy the string s1 to s2.
s2 = Copy(s1)
MsgBox(s2) 'Displays Hello World.
Remove
Remove 函數從指定字串的指定位置開始刪除指定數目的字元。Remove 函數有兩個參數。分別是 StartIndex 參數和 Count 參數。Startindex 參數指定開始刪除字元的字串位置。Count 參數指定要刪除的字元數。
Remove 樣本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "Hello World"
'Removes 3 characters starting from character e.
s2 = s1.Remove(1, 3)
MsgBox(s2) 'Displays Hello World.
Substring
Substring 函數從指定字串的指定位置開始檢索字串。
Substring 樣本:
以下樣本將從指定的字元位置開始並按指定的長度來檢索子字串:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "Hello World"
s2 = s1.Substring(6, 5) 'Returns World.
MsgBox(s2)
ToCharArray
ToCharArray 函數將字串中的字元複製到 Unicode 字元數組中。
ToCharArray 樣本:
以下樣本將指定位置中的字元複製到 Character 數組中:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1 As String
Dim ch(10) As Char
s1 = "Hello World"
'Copies the characters starting from W to d to a Character array.
ch = s1.ToCharArray(6, 5)
MsgBox(ch(3)) 'Displays l.
ToLower
ToLower 函數採用一個字串作為參數,然後以小寫形式返回該字串的副本。
ToLower 樣本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "Hello World"
s2 = s1.ToLower() 'Converts any uppercase characters to lowercase.
MsgBox(s2) 'Displays hello world.
ToUpper
ToUpper 函數採用一個字串作為參數,然後以大寫形式返回該字串的副本。
ToUpper 樣本:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = "Hello World"
s2 = s1.ToUpper() 'Converts any lowercase characters to uppercase.
MsgBox(s2) 'Displays HELLO WORLD.
Trim、TrimStart 和 TrimEnd
這些函數會返回一個包含指定字串的副本的字串。使用 Trim 函數時,既沒有起始空格也沒有尾隨空格。使用 TrimStart 函數時,沒有起始空格。使用 TrimEnd 函數時,沒有尾隨空格。
例如:
以下樣本使用 TrimStart 函數刪除字串變數開始處的空格,使用 TrimEnd 函數刪除字串變數末尾的空格,以及使用 Trim 函數刪除字串變數中的起始空格和尾隨空格:
' Code is not compiled unless it is put in a Sub or in a Function.
Dim s1, s2 As String
s1 = " Hello World "
s2 = s1.Trim() 'Returns Hello World without any white spaces.
s2 = s1.TrimStart 'Removes the spaces at the start.
s2 = s1.TrimEnd 'Removes the white spaces at the end.