VB基礎(VB字符串操作方法)

字號:

1.方法:
    SubStr() 中文化取子字串,相對Mid()
    Strlen() 中文化字串長度,相對Len()
    StrLeft() 中文化取左字串,相對Left()
    StrRight() 中文化取右字串,相對Right()
    isChinese() 檢查某個字是否中文字
    2.UniCode轉成ByteAry
    Dim byteAry() As Byte
    Dim str5 As String
    Dim i As Long str5 = "這abc"
    byteAry = str5
    For i = LBound(byteAry) To UBound(byteAry)
    Debug.Print byteAry(i) '得 25 144 97 0 98 0 99 0
    Next i
    Debug.Print Len(str5), LenB(str5) '得4 8 所以了,可看出UniCode 的特性,程式應改一下,使用Strconv()來轉換
    Dim byteAry() As Byte
    Dim str5 As String
    Dim i As Long str5 = "這abc"
    byteAry = StrConv(str5, vbFromUnicode)
    For i = LBound(byteAry) To UBound(byteAry)
    Debug.Print byteAry(i) '得 25 144 97 98 99
    Next i Debug.Print LenB(StrConv(str5, vbFromUnicode)) '得5
    3.ByteAry轉回UniCode 使用Strconv()轉換
    Dim byteAry(10) as Byte
    Dim Str5 as String
    byteAry(0) = 25
    byteAry(1) = 144
    byteAry(2) = 97
    byteAry(3) = 98
    byteAry(4) = 99
    Str5 = StrConv(byteAry, vbUniCode)
    4.0、""(空字串)、Null、Empty、與 Nothing 的區(qū)別
    Dim A
    Dim B As String
    Dim C As Integer
    Dim D As Object
    A 等于 Empty, 因為尚未初始化的「不定型變量」都等于 Empty。但如果檢測 A = "" 或 A = 0, 也都可以得到 True 值。
    B 等于 "", 因為尚未初始化的非固定長度「字串」都等于 "" 。 但請注意 B<> Null。
    C 等于 0,
    D 等于 Nothing, 尚未設定有物件的「物件變量」都等于 Nothing, 但請不要使用 D = Nothing , 而要使用 D Is Nothing 考試大提示來判斷 D 是否等于 Nothing, 因為判斷 是否相等的符號是 Is 不是 = 。
    最令人迷惑的地方是 Null 這個保留字, 請看以下語句:
    Print X = Null
    Print X <> Null
    結果都是輸出 Null(不是 True 也不是 False), 這是因為任何一個運算式只要含有 Null , 則該運算式就等于 Null, 實際上想要判斷某一數據是否為 Null 絕對不能使用:
    If X = Null Then ' 永遠都會得到 Null
    而要使用:
    If IsNull(X) Then
    哪一種數據會等于 Null 呢? 除了含有 Null 運算式之外, 就屬沒有輸入任何數據的「數據字段」(在數據庫中) 會等于 Null。