String 資料類型 (Visual Basic)String Data Type (Visual Basic)

保留不帶正負號16位(2位元組)程式碼點的序列,其範圍介於0到65535之間。Holds sequences of unsigned 16-bit (2-byte) code points that range in value from 0 through 65535. 每個程式代碼點或字元碼都代表一個 Unicode 字元。Each code point, or character code, represents a single Unicode character. 字串可包含0到大約2000000000(2 ^ 31)個 Unicode 字元。A string can contain from 0 to approximately two billion (2 ^ 31) Unicode characters.


您可以使用 String 資料類型來保存多個字元,而不會有 Char() 的陣列管理額外負荷,也就是 @no__t 2 元素的陣列。Use the String data type to hold multiple characters without the array management overhead of Char(), an array of Char elements.

@No__t-0 的預設值為 Nothing (null 參考)。The default value of String is Nothing (a null reference). 請注意,這與空字串(值 "")不同。Note that this is not the same as the empty string (value "").

Unicode 字元Unicode Characters

Unicode 的第一個128程式碼點(0–127)對應到標準美式鍵盤上的字母和符號。The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. 這些前128個程式碼點與 ASCII 字元集所定義的相同。These first 128 code points are the same as those the ASCII character set defines. 第二個128程式碼片段(128–255)代表特殊字元,例如以拉丁為基礎的字母、重音、貨幣符號和分數。The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode 會針對各種不同的符號使用其餘的程式碼點(256-65535)。Unicode uses the remaining code points (256-65535) for a wide variety of symbols. 這包括全球文字字元、變音符號和數學和技術符號。This includes worldwide textual characters, diacritics, and mathematical and technical symbols.

您可以針對 @no__t 2 變數中的個別字元,使用 IsDigitIsPunctuation 之類的方法來判斷其 Unicode 分類。You can use methods such as IsDigit and IsPunctuation on an individual character in a String variable to determine its Unicode classification.

格式需求Format Requirements

您必須將 String 常值括在引號內(" ")。You must enclose a String literal within quotation marks (" "). 如果您必須在字串中包含引號做為其中一個字元,您可以使用兩個連續的引號("")。If you must include a quotation mark as one of the characters in the string, you use two contiguous quotation marks (""). 下列範例將說明這點。The following example illustrates this.

Dim j As String = "Joe said ""Hello"" to me."  
Dim h As String = "Hello"  
' The following messages all display the same thing:  
' "Joe said "Hello" to me."  
MsgBox("Joe said " & """" & h & """" & " to me.")  
MsgBox("Joe said """ & h & """ to me.")  

請注意,在字串中代表引號的連續引號,與開始和結束 @no__t 0 常值的引號無關。Note that the contiguous quotation marks that represent a quotation mark in the string are independent of the quotation marks that begin and end the String literal.

字串操作String Manipulations

將字串指派給 String 變數之後,該字串是不可變的,這表示您無法變更其長度或內容。Once you assign a string to a String variable, that string is immutable, which means you cannot change its length or contents. 當您以任何方式改變字串時,Visual Basic 會建立新的字串,並放棄上一個字串。When you alter a string in any way, Visual Basic creates a new string and abandons the previous one. @No__t-0 變數會指向新的字串。The String variable then points to the new string.

您可以使用各種字串函式來操作 @no__t 0 變數的內容。You can manipulate the contents of a String variable by using a variety of string functions. 下列範例說明 Left 函式The following example illustrates the Left function

Dim S As String = "Database"  
' The following statement sets S to a new string containing "Data".  
S = Microsoft.VisualBasic.Left(S, 4)  

另一個元件所建立的字串可能會以前置或尾端空格填補。A string created by another component might be padded with leading or trailing spaces. 如果您收到這類字串,您可以使用 TrimLTrim 和 @no__t 2 函式來移除這些空格。If you receive such a string, you can use the Trim, LTrim, and RTrim functions to remove these spaces.

如需有關字串操作的詳細資訊,請參閱字串For more information about string manipulations, see Strings.

程式設計提示Programming Tips

  • 負數。Negative Numbers. 請記住,String 所保留的字元是不帶正負號的,而且不能代表負數值。Remember that the characters held by String are unsigned and cannot represent negative values. 在任何情況下,您不應該使用 String 來保存數值。In any case, you should not use String to hold numeric values.

  • Interop 考慮。Interop Considerations. 如果您要使用的元件不是針對 .NET Framework 所撰寫(例如 Automation 或 COM 物件),請記住,在其他環境中,字串字元具有不同的資料寬度(8位)。If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, remember that string characters have a different data width (8 bits) in other environments. 如果您要將8位字元的字串引數傳遞給這類元件,請將它宣告為 Byte(),@no__t 1 元素的陣列,而不是在新的 Visual Basic 程式碼中 StringIf you are passing a string argument of 8-bit characters to such a component, declare it as Byte(), an array of Byte elements, instead of String in your new Visual Basic code.

  • 輸入字元。Type Characters. 將識別項型別字元 $ 附加至任何識別碼,會強制其為 String 資料類型。Appending the identifier type character $ to any identifier forces it to the String data type. String 沒有常數值型別字元。String has no literal type character. 不過,編譯器會將括在引號(" ")的常值視為 StringHowever, the compiler treats literals enclosed in quotation marks (" ") as String.

  • 架構類型。Framework Type. .NET Framework 中的對應類型是 System.String 類別。The corresponding type in the .NET Framework is the System.String class.

另請參閱See also