2012-11-02 82 views

回答

25

最安全&最簡單的就是循環;

Dim buff() As String 
ReDim buff(Len(my_string) - 1) 
For i = 1 To Len(my_string) 
    buff(i - 1) = Mid$(my_string, i, 1) 
Next 

如果您保證只使用ansi字符,您可以;

Dim buff() As String 
buff = Split(StrConv(my_string, vbUnicode), Chr$(0)) 
ReDim Preserve buff(UBound(buff) - 1) 
+0

使用Unicode時,我們會做什麼? – mgae2m

8

下面是在VBA中執行此操作的另一種方法。

Function CharacterArray(value As String) 
    value = StrConv(value, vbUnicode) 
    CharacterArray = Split(Left(value, Len(value) - 1), vbNullChar) 
End Function 
Sub example() 
    Dim d As String 
    Dim myArray() 
    myArray = CharacterArray("hi there") 
End Sub 
11

您可以將字符串分配給字節數組(反之亦然)。其結果是爲每個字符2個的數字,所以聖誕轉換爲含有一個字節數組{88,0,109,0,97,0,115,0}
或者可以使用中StrConv

Dim bytes() as Byte 
bytes = StrConv("Xmas", vbFromUnicode) 

,這將給你{88109, 97,115},但在這種情況下,您不能將字節數組分配回字符串。
您可以使用Chr()函數將字節數組中的數字轉換回字符

0

問題是沒有內置方法(或者至少我們沒有人能找到一個)在vb中執行此操作。但是,有一個在空間上分割字符串,所以我只重建字符串並在空格中添加....

Private Function characterArray(ByVal my_string As String) As String() 
    'create a temporary string to store a new string of the same characters with spaces 
    Dim tempString As String = "" 
    'cycle through the characters and rebuild my_string as a string with spaces 
    'and assign the result to tempString. 
    For Each c In my_string 
    tempString &= c & " " 
    Next 
    'return return tempString as a character array. 
    Return tempString.Split() 
End Function