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

字號(hào):

1.方法:
    SubStr() 中文化取子字串,相對(duì)Mid()
    Strlen() 中文化字串長(zhǎng)度,相對(duì)Len()
    StrLeft() 中文化取左字串,相對(duì)Left()
    StrRight() 中文化取右字串,相對(duì)Right()
    isChinese() 檢查某個(gè)字是否中文字
    2.UniCode轉(zhuǎn)成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 的特性,程式應(yīng)改一下,使用Strconv()來(lái)轉(zhuǎn)換
    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轉(zhuǎn)回UniCode 使用Strconv()轉(zhuǎn)換
    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, 因?yàn)樯形闯跏蓟摹覆欢ㄐ妥兞俊苟嫉扔?Empty。但如果檢測(cè) A = "" 或 A = 0, 也都可以得到 True 值。
    B 等于 "", 因?yàn)樯形闯跏蓟姆枪潭ㄩL(zhǎng)度「字串」都等于 "" 。 但請(qǐng)注意 B<> Null。
    C 等于 0,
    D 等于 Nothing, 尚未設(shè)定有物件的「物件變量」都等于 Nothing, 但請(qǐng)不要使用 D = Nothing , 而要使用 D Is Nothing 考試大提示來(lái)判斷 D 是否等于 Nothing, 因?yàn)榕袛?是否相等的符號(hào)是 Is 不是 = 。
    最令人迷惑的地方是 Null 這個(gè)保留字, 請(qǐng)看以下語(yǔ)句:
    Print X = Null
    Print X <> Null
    結(jié)果都是輸出 Null(不是 True 也不是 False), 這是因?yàn)槿魏我粋€(gè)運(yùn)算式只要含有 Null , 則該運(yùn)算式就等于 Null, 實(shí)際上想要判斷某一數(shù)據(jù)是否為 Null 絕對(duì)不能使用:
    If X = Null Then ' 永遠(yuǎn)都會(huì)得到 Null
    而要使用:
    If IsNull(X) Then
    哪一種數(shù)據(jù)會(huì)等于 Null 呢? 除了含有 Null 運(yùn)算式之外, 就屬?zèng)]有輸入任何數(shù)據(jù)的「數(shù)據(jù)字段」(在數(shù)據(jù)庫(kù)中) 會(huì)等于 Null。