Option ExplicitEnum CharType ctSpliter ctAnsi ctUnicode End EnumSub Main() Dim aWords() As String aWords = SplitWords("汉中工349803中", 2) Debug.Print Join(aWords, vbCrLf) End SubSub ArrayAdd(a() As String, Count As Long, ByVal Value As String) Count = Count + 1 ReDim Preserve a(Count - 1) a(Count - 1) = Value End SubFunction SplitWords(ByVal Text As String, ByVal UniWordLen As Long) As String() Dim aWords() As String Dim lWordCount As Long Dim ch As String Dim sWord As String Dim eLastType As CharType Dim eNewType As CharType Dim i As Long For i = 1 To Len(Text) ch = Mid$(Text, i, 1) If (AscW(ch) And &HFF00) = 0 Then Select Case ch Case "(", ")" eNewType = ctSpliter Case Else eNewType = ctAnsi End Select Else eNewType = ctUnicode End If If eNewType <> eLastType Then If eLastType <> ctSpliter Then If Len(sWord) <> 0 Then ArrayAdd aWords, lWordCount, sWord End If End If If eNewType = ctSpliter Then sWord = vbNullString Else sWord = ch End If eLastType = eNewType Else If eLastType <> ctSpliter Then sWord = sWord & ch If (eLastType = ctUnicode) And (Len(sWord) = UniWordLen) Then ArrayAdd aWords, lWordCount, sWord sWord = vbNullString End If End If End If Next If eLastType <> ctSpliter Then If Len(sWord) <> 0 Then ArrayAdd aWords, lWordCount, sWord End If End If SplitWords = aWords End Function
Private Sub Command3_Click() Dim strSource As String Dim lngPos As Long: lngPos = 1 Dim strNumber As String
strSource = "汉中工349803中(某人某事某时间111某天)什么啊这是" Do While lngPos <= Len(strSource) If WhatChar(Mid(strSource, lngPos, 1)) = 0 Then '汉字 If lngPos + 1 <= Len(strSource) Then If WhatChar(Mid(strSource, lngPos + 1, 1)) = 0 Then Debug.Print Mid(strSource, lngPos, 2) lngPos = lngPos + 2 Else Debug.Print Mid(strSource, lngPos, 1) lngPos = lngPos + 1 End If Else Debug.Print Mid(strSource, lngPos, 1) lngPos = lngPos + 1 End If ElseIf WhatChar(Mid(strSource, lngPos, 1)) = 2 Then '字母或符号 Debug.Print Mid(strSource, lngPos, 1) lngPos = lngPos + 1 Else '数字 strNumber = "" Do strNumber = strNumber & Mid(strSource, lngPos, 1) lngPos = lngPos + 1 Loop Until WhatChar(Mid(strSource, lngPos, 1)) <> 1 Debug.Print strNumber End If Loop End Sub Private Function WhatChar(ByVal vStr As String) As Integer Dim gbascii As Byte Dim intChar As Integer If Asc(vStr) < 0 Then gbascii = AscB(StrConv(vStr, vbFromUnicode)) '区位码在16区之后的为汉字 If gbascii - 160 > 15 Then intChar = 0 ' "是汉字" Else intChar = 2 ' "是全角符号" End If Else intChar = 1 ' "是半角英文或数字" End If WhatChar = intChar End Function
说个思路吧。1 首先用 Replace() 函数将串中的 ")" 全部替换成 "("。然后用 Split(strSource, "(") 将初始串分割成子串。2 循环遍历各个子串。遇到符合上述分隔规则的分界点,或直接切割,或在串中插入分割符(如"(")最后 Split。
ctSpliter
ctAnsi
ctUnicode
End EnumSub Main()
Dim aWords() As String
aWords = SplitWords("汉中工349803中", 2)
Debug.Print Join(aWords, vbCrLf)
End SubSub ArrayAdd(a() As String, Count As Long, ByVal Value As String)
Count = Count + 1
ReDim Preserve a(Count - 1)
a(Count - 1) = Value
End SubFunction SplitWords(ByVal Text As String, ByVal UniWordLen As Long) As String()
Dim aWords() As String
Dim lWordCount As Long
Dim ch As String
Dim sWord As String
Dim eLastType As CharType
Dim eNewType As CharType
Dim i As Long For i = 1 To Len(Text)
ch = Mid$(Text, i, 1)
If (AscW(ch) And &HFF00) = 0 Then
Select Case ch
Case "(", ")"
eNewType = ctSpliter
Case Else
eNewType = ctAnsi
End Select
Else
eNewType = ctUnicode
End If
If eNewType <> eLastType Then
If eLastType <> ctSpliter Then
If Len(sWord) <> 0 Then
ArrayAdd aWords, lWordCount, sWord
End If
End If If eNewType = ctSpliter Then
sWord = vbNullString
Else
sWord = ch
End If
eLastType = eNewType
Else
If eLastType <> ctSpliter Then
sWord = sWord & ch
If (eLastType = ctUnicode) And (Len(sWord) = UniWordLen) Then
ArrayAdd aWords, lWordCount, sWord sWord = vbNullString
End If
End If
End If
Next If eLastType <> ctSpliter Then
If Len(sWord) <> 0 Then
ArrayAdd aWords, lWordCount, sWord
End If
End If SplitWords = aWords
End Function
Dim strSource As String
Dim lngPos As Long: lngPos = 1
Dim strNumber As String
strSource = "汉中工349803中(某人某事某时间111某天)什么啊这是"
Do While lngPos <= Len(strSource)
If WhatChar(Mid(strSource, lngPos, 1)) = 0 Then '汉字
If lngPos + 1 <= Len(strSource) Then
If WhatChar(Mid(strSource, lngPos + 1, 1)) = 0 Then
Debug.Print Mid(strSource, lngPos, 2)
lngPos = lngPos + 2
Else
Debug.Print Mid(strSource, lngPos, 1)
lngPos = lngPos + 1
End If
Else
Debug.Print Mid(strSource, lngPos, 1)
lngPos = lngPos + 1
End If
ElseIf WhatChar(Mid(strSource, lngPos, 1)) = 2 Then '字母或符号
Debug.Print Mid(strSource, lngPos, 1)
lngPos = lngPos + 1
Else '数字
strNumber = ""
Do
strNumber = strNumber & Mid(strSource, lngPos, 1)
lngPos = lngPos + 1
Loop Until WhatChar(Mid(strSource, lngPos, 1)) <> 1
Debug.Print strNumber
End If
Loop
End Sub
Private Function WhatChar(ByVal vStr As String) As Integer
Dim gbascii As Byte
Dim intChar As Integer
If Asc(vStr) < 0 Then
gbascii = AscB(StrConv(vStr, vbFromUnicode))
'区位码在16区之后的为汉字
If gbascii - 160 > 15 Then
intChar = 0 ' "是汉字"
Else
intChar = 2 ' "是全角符号"
End If
Else
intChar = 1 ' "是半角英文或数字"
End If
WhatChar = intChar
End Function