字串與其他類型之間的轉換 (Visual Basic)Conversions Between Strings and Other Types (Visual Basic)

您可以將轉換數字Boolean,或日期/時間值StringYou can convert a numeric, Boolean, or date/time value to a String. 您也可以反向轉換,從數值字串值Boolean,或Date— 提供字串的內容可以解譯為有效的值的目的地資料類型。You can also convert in the reverse direction — from a string value to numeric, Boolean, or Date — provided the contents of the string can be interpreted as a valid value of the destination data type. 如果它們無法在執行階段錯誤發生。If they cannot, a run-time error occurs.

所有這些指派,任一方向的轉換縮小轉換。The conversions for all these assignments, in either direction, are narrowing conversions. 您應該使用類型轉換關鍵字 (CBoolCByteCDateCDblCDecCIntCLngCSByteCShortCSngCStrCUIntCULngCUShort,和CType)。You should use the type conversion keywords (CBool, CByte, CDate, CDbl, CDec, CInt, CLng, CSByte, CShort, CSng, CStr, CUInt, CULng, CUShort, and CType). FormatVal函式可讓您進一步控制字串和數字之間的轉換。The Format and Val functions give you additional control over conversions between strings and numbers.

如果您已定義類別或結構,您可以定義之間的型別轉換運算子String和您的類別或結構的類型。If you have defined a class or structure, you can define type conversion operators between String and the type of your class or structure. 如需詳細資訊,請參閱如何:定義轉換運算子For more information, see How to: Define a Conversion Operator.

數字轉換為字串Conversion of Numbers to Strings

您可以使用Format函式,將數字轉換為格式化的字串,其中可以包括不僅適當的數字,但也格式化符號,例如貨幣符號 (例如$),千分位分隔符號或數字分位符號(例如,),以及小數分隔符號 (例如.)。You can use the Format function to convert a number to a formatted string, which can include not only the appropriate digits but also formatting symbols such as a currency sign (such as $), thousands separators or digit grouping symbols (such as ,), and a decimal separator (such as .). Format 會自動使用適當的符號,根據地區選項Windows 控制台中控制台Format automatically uses the appropriate symbols according to the Regional Options settings specified in the Windows Control Panel.

請注意,串連 (&) 運算子可以將數字轉換成字串以隱含方式,如下列範例所示。Note that the concatenation (&) operator can convert a number to a string implicitly, as the following example shows.

' The following statement converts count to a String value.  
Str = "The total count is " & count  

字串轉換成數字Conversion of Strings to Numbers

您可以使用Val函式來明確地轉換為數字的字串中的數字。You can use the Val function to explicitly convert the digits in a string to a number. Val 讀取的字串,直到遇到數字、 空格、 索引標籤、 換行字元或句號以外的字元。Val reads the string until it encounters a character other than a digit, space, tab, line feed, or period. 序列"& O"和"& H"alter 基底的數字系統,並終止掃描。The sequences "&O" and "&H" alter the base of the number system and terminate the scanning. 它會停止讀取,直到Val將所有適當的字元轉換為數值。Until it stops reading, Val converts all appropriate characters to a numeric value. 例如,下列陳述式會傳回值141.825For example, the following statement returns the value 141.825.

Val(" 14 1.825 miles")

當 Visual Basic 會將字串轉換為數字值時,它會使用地區選項指定在 Windows 中設定控制台解譯千分位分隔符號,十進位分隔符號,並貨幣符號。When Visual Basic converts a string to a numeric value, it uses the Regional Options settings specified in the Windows Control Panel to interpret the thousands separator, decimal separator, and currency symbol. 這表示,轉換可能成功下其中一個設定。This means that a conversion might succeed under one setting but not another. 比方說,"$14.20"是可接受在英文 (美國) 地區設定,但不是在法文地區設定。For example, "$14.20" is acceptable in the English (United States) locale but not in any French locale.

另請參閱See also