当前位置:编程学习 > VB >>

關于將字符串內字符轉成數組

如一個變量 strType = "<A1>212<B1:C2>222<D1,D3>"
請教怎樣將strType里面被<>包含的字符拿出來放到數組呢?
結果
astrTemp(0)="<A1>"
astrTemp(1)="<B1:C2>
astrTemp(2)="<D1,D3>" --------------------编程问答-------------------- Option Explicit

Private Sub Command1_Click()
Dim strP() As String
Dim strT() As String
Dim astrTemp(0 To 2) As String
Dim strType As String
strType = "<A1>212 <B1:C2>222 <D1,D3>"
strP = Split(strType, "212")
strT = Split(strP(1), "222")
astrTemp(0) = strP(0)
astrTemp(1) = strT(0)
astrTemp(2) = strT(1)
Debug.Print astrTemp(0)
Debug.Print astrTemp(1)
Debug.Print astrTemp(2)
End Sub --------------------编程问答--------------------
引用 1 楼 veron_04 的回复:
Option Explicit

Private Sub Command1_Click()
Dim strP() As String
Dim strT() As String
Dim astrTemp(0 To 2) As String
Dim strType As String
strType = " <A1>212 <B1:C2>222 <D1,D3>"
strP = Split(strType, "212")
strT = Split(strP(1), "222")
astrTemp(0) = strP(0)
astrTemp(1) = strT(0)
astrTemp(2) = strT(1)
Debug.Print astrTemp(0)
Debug.Print astrTemp(1)
Debug.Print astrTemp(2)
End Sub


我的字符串是舉例的··中間的字符不知量· --------------------编程问答--------------------
Private Sub Form_Load()
'引用Microsoft VBScript Regular Expressions 5.5
Dim re As RegExp
Dim astrTemp()
Dim i
    Dim mh As Match
    Dim mhs As MatchCollection
    Dim strType As String
        strType = " <A1>212 <B1:C2>222 <D1,D3>"
    Set re = New RegExp
    re.Global = True
     re.Pattern = "<[\u4e00-\u9fa5\S]*>"         '同样是匹配地址,注意和上例的不同
    Set mhs = re.Execute(strType)
For Each mh In mhs
ReDim Preserve astrTemp(i)
astrTemp(i) = mh
Debug.Print astrTemp(i)
i = i + 1
 Next
End Sub
 
--------------------编程问答--------------------

    dim strType as string
    dim tmp
    dim i as long 
    strType = " <A1>212 <B1:C2>222 <D1,D3>"  
    tmp=split(trim(strType),"<")
    for i=1 to ubound(tmp)
        tmp(i)="<" & mid(tmp(i),1,instr(tmp(i),">"))
    next
    debug.? join(tmp)
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,