[菜鳥級別]TXT文件內容形式的改變
我想把Table_1的文件變成Table_2的樣子。
Table_1的情況:
①一行列名,一行內容
②不同記錄用空行隔開
③列名之間的空格沒有規律性(可能是一個空格,也可能是2、3個空格)
④內容之間的空格沒有規律性(可能是一個空格,也可能是2、3個空格)
Table_1.txt:
idnamesexage
01LiLeiM16
Heightweight
17866
idnamesexage
02HanMeimeiF15
Heightweight
16851
希望變成Table_2的樣子:
①只有一行列名放在第一行
②沒有空行
③列名之間只有一個空格
④內容之間只有一個空格
Table_2.txt:
Id name sex age Height weight
01 LiLei M 16 178 66
02 HanMeimei F 15 168 51
[解决办法]
Private Sub Command1_Click()Dim s As String, i As Byte, n As Byte, a, aTmp, sTmp1 As String, sTmp2 As String s = Text1.Text a = Split(s, vbCrLf & vbCrLf) For i = 0 To UBound(a) aTmp = Split(a(i), vbCrLf) For n = 0 To UBound(aTmp) s = aTmp(n) Do While InStr(s, " ") > 0 '2个空格替换为一个 s = Replace(s, " ", " ") Loop If n Mod 2 = 0 Then '奇数行 If InStr(sTmp1, Trim(s)) = 0 Then sTmp1 = sTmp1 & Trim(s) & " " Else '偶数行 sTmp2 = sTmp2 & Trim(s) & " " End If Next sTmp2 = sTmp2 & vbCrLf Next Text2.Text = sTmp1 & vbCrLf & sTmp2End SubPrivate Sub Form_Load() Text1.Text = "id name sex age" & vbCrLf & "01 LiLei M 16" & vbCrLf & "Height Weight" & vbCrLf & "178 66" & vbCrLf _ & vbCrLf & "id name sex age" & vbCrLf & "02 HanMeimei F 15" & vbCrLf & "Height Weight" & vbCrLf & "168 51"End Sub