Asc、AscW 函式
更新:2007 年 11 月
傳回 Integer 值,這個值表示與字元對應的字元碼。
Public Overloads Function Asc(ByVal String As Char) As Integer
Public Overloads Function AscW(ByVal String As Char) As Integer
' -or-
Public Overloads Function Asc(ByVal String As String) As Integer
Public Overloads Function AscW(ByVal String As String) As Integer
參數
- String
必要項。任何有效的 Char 或 String 運算式。如果 String 是 String 運算式,則只有字串的第一個字元用於輸入。如果 String 為 Nothing 或未包含字元,則會發生 ArgumentException 錯誤。
例外狀況
例外狀況類型 |
錯誤代碼 |
條件 |
---|---|---|
String 是空的或長度為零。 |
如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以Visual Basic 的結構化例外處理概觀取代這類錯誤控制項。
備註
Asc 會傳回輸入字元的「字碼指標」(Code Point) 或字元碼。這可以是值從 0 到 255 的單一位元組字元集 (Single-Byte Character Set,SBCS),和值從 -32768 到 32767 的雙位元組字元集 (DBCS)。
傳回值是取決於目前執行緒的字碼頁 (Code Page),而該字碼頁會包含在 System.Globalization 命名空間中 TextInfo 類別的 ANSICodePage 屬性 (Property) 中。指定 System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage,即可取得 ANSICodePage。
AscW 會傳回輸入字元的 Unicode 字碼指標。這可以是 0 到 65535。傳回值和文化特性 (Culture) 及目前執行緒的字碼頁都無關。
注意事項: |
---|
Visual Basic 舊版的 AscB 函式會傳回位元組的程式碼,而不是字元。這項功能主要用來轉換雙位元組字元集 (DBCS) 應用程式中的字串。從 Visual Basic 2005 開始,字串的格式都是 Unicode,而且不再支援 AscB。 |
範例
下列範例會使用 Asc 函式,傳回與每個字串中第一個字母對應的 Integer 資料型別 (Visual Basic) 字元碼。
Dim codeInt As Integer
' The following line of code sets codeInt to 65.
codeInt = Asc("A")
' The following line of code sets codeInt to 97.
codeInt = Asc("a")
' The following line of code sets codeInt to 65.
codeInt = Asc("Apple")
需求
命名空間 (Namespace)︰Microsoft.VisualBasic
模組:Strings
組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)