作者Marty (DNA探针)
看板Visual_Basic
标题Re: [VBA ] 请问如何剖析文字串
时间Mon Apr 9 11:25:55 2007
※ 引述《fumizuki (蒙面加菲狮)》之铭言:
: ※ 引述《myidname (你碰不得的男人)》之铭言:
: : 请问版上大大
: : 若我有一个string被空白或者豆号分开
: : 示意如下 " year , what line go "
: : 这个自串前後都有空白,其间使用空白或豆号隔开
: : 当空白或豆号的位置不定
: : 要如何解析出year,what,line,go这四个不同单字
: : 感激不尽~
: Public Function Parse(ByVal src As String) As Variant
: Dim i1 As Integer, c As String, s As String
: Dim arr As Variant, e As Variant, result As String
: arr = Split(src, vbCrLf) '假设有换行符号存在
: For Each e In arr
: e = Trim(e): i2 = 1
: Do While Len(e) > 0
: i1 = i1 + 1
: If i1 > Len(e) Then c = " " Else c = Mid(e, i1, 1)
: If c = " " Or c = "," Then
: s = Left(e, i1 - 1): e = Trim(Mid(e, i1 + 1)): i1 = 0
: If s <> "" Then
: If result <> "" Then result = result & ","
: result = result & s
: End If
: End If
: Loop
: Next
: Parse = Split(result, ",") '传回一个阵列
: End Function
参考f大的code,做了一些修改:
'**********************************************************
Public Function Parse(ByVal src As String) As Variant
Dim result_temp As String
Dim e As Variant, sti_temp As Variant
'先用逗号取代空白,再用逗号解析字串
sti_temp = Split(Replace(src, " ", ","), ",")
'把Null的值去掉
For Each e In sti_temp
If e <> "" Then
result = result & e & ","
End If
Next
'传回结果阵列
Parse = Split(Left(result, Len(result) - 1), ",")
End Function
'**********************************************************
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 211.77.241.1
※ 编辑: Marty 来自: 211.77.241.1 (04/09 11:29)