個人也對字串運用下過一份苦心
不錯用的說
比如
strTemp=StringSplit_Str("c:\abc\temp\test.txt", "1\0$\", 0)
結果strTemp="test.txt",將後面的 0 改成 1 就變成 "c:\abc\temp\"
strTemp=StringSplit_Str("test.txt", "1\0$.", 0)
則可得副檔名 strTemp="txt"
strTemp=StringSplit_Str("test.exe 11,22,33", ",", 2)
則strTemp= "22",改成 3 則是 "33"
strText="a,b,c,d,e"
strTemp=StringSplit_Str(strText, ",", 0)
strTemp=StringSplit_Str(strText, ",")
strTemp=StringSplit_Str(strText, ",")
strTemp=StringSplit_Str(strText, ",")
strTemp=StringSplit_Str(strText, ",")
則strTemp依序得到 "a" "b" "c" "d" "e"
涵式如下
===============
Public Function StringSplit_Str(ByVal TextTemp, ByVal NOStringIs, Optional ByVal StartNO As Long)
Dim CharNO As Long
Static no As Long
If Mid$(NOStringIs, 1, 3) = "1\0" Then GoTo AgainstSearchLABLE
If StartNO = 0 Then
no = no + 1
StartNO = Abs(no)
ElseIf StartNO > 0 Then
no = 0
End If
If TextTemp = "" Then Exit Function
Do
StartNO = StartNO - 1
CharNO = InStr(1, TextTemp, NOStringIs, vbBinaryCompare)
If CharNO > 1 Then
StringSplit_Str = Mid$(TextTemp, 1, CharNO - 1)
ElseIf CharNO = 0 Then
StringSplit_Str = TextTemp
ElseIf CharNO = 1 Then
StringSplit_Str = ""
End If
TextTemp = Mid$(TextTemp, CharNO + Len(NOStringIs)) 'trim
If CharNO = 0 And StartNO <> 0 Then
StringSplit_Str = ""
Exit Function
End If
Loop Until StartNO = 0
Exit Function
AgainstSearchLABLE:
'逆搜尋字串 NOStringIs
If TextTemp = "" Then Exit Function
no = Len(TextTemp)
NOStringIs = Mid$(NOStringIs, 5)
CharNO = InStrRev(TextTemp, NOStringIs, no, vbBinaryCompare)
CharNO = CharNO - 1
If StartNO = 1 Then '路徑
StringSplit_Str = Mid$(TextTemp, 1, CharNO + Len(NOStringIs))
ElseIf StartNO = 0 Then '檔名
StringSplit_Str = Mid$(TextTemp, CharNO + Len(NOStringIs) + 1)
End If
End Function
|