【已解決!】怎樣將一文本中的每一次前后都加上逗號。
本帖最后由 wenquan79 于 2010-7-29 14:08 编辑大家好!請教一下,如何將一文本的每行前后都加逗號,謝謝!
如下文本的內容如下:
------------------
C1
C2
C3
C4
C5
-----
想變成的效果:
----------------
,C1,
,C2,
,C3,
,C4,
,,
,C5,
------------------- $file = FileOpen("test.txt", 0)
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
$w = FileWriteLine("test2.txt",","&$line&",")
Wend把帮助里FileReadLine的列子稍微改一下 本帖最后由 xsjtxy 于 2010-7-28 15:13 编辑
$file = FileOpen("test.txt", 0)
If $file = -1 Then
Exit
EndIf
$file2 = FileOpen("test.tmp", 2)
If $file2 = -1 Then
Exit
EndIf
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
if StringLeft($line, 1) <> "," then $line = ","&$line
if StringRight($line, 1) <> "," then $line = $line&","
FileWriteLine($file2, $line)
Wend
FileClose($file)
FileClose($file2)
FileMove("test.tmp","test.txt",1)
msgbox(0,"","完成") 回复 3# xsjtxy
感謝大家請熱情回復,Ziya兄及Xsjtxy兄的代碼都可以實現此功能。
再請教一下,若反過來,這個功能又怎樣才能實現呢?
即判斷一文本內的每行,如果是以逗號開頭就將前逗號去掉,以逗號結尾的也結尾的逗號去掉,
TEST.txt內容如下:
,c1,
,c2,c3,c4,c5,
c88,c99,
,r65,r53,c46
,r46,
期望可以變成
C1
c2,c3,c4,c5
c88,c99
r65,r53,c46
r46
------------------ #3 Xsjtxy的代码是判断如果没有,就加上,
你反过来不就行了么..... 给个思路:stringleft()判断头,stringright()判断尾
也可用stringmid()判断。 回复 6# 3mile
謝謝大家的回復使我的問題得以解決,再次感謝!
在Xsjtxy兄的代碼上改一下就可以了,用StringTrimLeft及StringTrimRight。$file = FileOpen("test.txt", 0)
If $file = -1 Then
Exit
EndIf
$file2 = FileOpen("test.tmp", 2)
If $file2 = -1 Then
Exit
EndIf
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
if StringLeft($line, 1) = "," then $line = StringTrimLeft($line, 1)
if StringRight($line, 1) = "," then $line = StringTrimRight($line, 1)
FileWriteLine($file2, $line)
Wend
FileClose($file)
FileClose($file2)
FileMove("test.tmp","test.txt",1)
msgbox(0,"","完成") 回复xsjtxy
感謝大家請熱情回復,Ziya兄及Xsjtxy兄的代碼都可以實現此功能。
再請教一下,若反過來,這 ...
wenquan79 发表于 2010-7-28 19:52 http://www.autoitx.com/images/common/back.gif$Str = _
',c1,' & @CRLF & _
',c2,c3,c4,c5,' & @CRLF & _
'c88,c99,' & @CRLF & _
'' & @CRLF & _
',r65,r53,c46' & @CRLF & _
',r46,'
Msgbox(0, '原字符串 ', $str)
$str = StringRegExpReplace($str, '(?m)^,|,(?=\r?\n)|,$', '')
Msgbox(0, '删除首尾半角逗号', $str)
页:
[1]