看板 Visual_Basic 關於我們 聯絡資訊
※ 引述《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)