文本处理问题(50)

  • 主题发起人 主题发起人 huxhang
  • 开始时间 开始时间
H

huxhang

Unregistered / Unconfirmed
GUEST, unregistred user!
将以下文本:"10BET","01","","1","""10BET","02","","","0""10BET","03","","1","""10BET","04","","1","""wBET","01","3","","""wBET","02","3","","""wBET","03","","","0""wBET","04","3","","""Astrabet","01","","1","""Astrabet","02","","1","""Astrabet","03","","","0""Astrabet","04","","","0"处理为:10BET:1,0,1,1wBET:3,3,0,3Astrabet:1,1,0,0目前错误的处理后:wBET:1,0,1,1Astrabet:3,3,0,31,1,0,0下面哪儿错了?procedure FormatStrs(AListSource, AListTarget: TStrings);const Cons_Space = '*';var SListSrcWord, SListTargetWord: TStringList
Sect: string
i, j: Integer
KeyWord: string
begin_word:string;begin Sect := ''
SListTargetWord := TStringList.Create
SListSrcWord := TStringList.Create
AListTarget.BeginUpdate
AListTarget.Clear
try for i := 0 to 3 do begin SListTargetWord.Add(Cons_Space)
end
for i := 0 to AListSource.Count - 1 do begin SListSrcWord.CommaText := AListSource
if SListSrcWord[0] <> Sect then begin AListTarget.Add(SListTargetWord.CommaText)
Sect := SListSrcWord[0]
for j := 1 to SListTargetWord.Count - 1 do begin SListTargetWord[j] := Cons_Space
begin_word:= Format('%s', [SListSrcWord[0]])
end
end
KeyWord := Format('%s%s%s', [SListSrcWord[2], SListSrcWord[3], SListSrcWord[4]])
begin_word:= Format('%s', [SListSrcWord[0]])
if (KeyWord = '') then begin KeyWord := Cons_Space
end
SListTargetWord[StrToInt(SListSrcWord[1]) - 1] := KeyWord
end
// AListTarget.Text:=begin_word+':'
AListTarget.Add(begin_word+':'+SListTargetWord.CommaText)
finally SListTargetWord.Free
SListSrcWord.Free
end
if AListTarget.Count > 0 then begin AListTarget.Delete(0)
end
AListTarget.EndUpdate;end;procedure TForm1.btn1Click(Sender: TObject);begin FormatStrs(Mmo1.Lines, Mmo2.Lines);end;
 
兄弟你只有一个AListTarget.Add(begin_word+':'+SListTargetWord.CommaText);让程序如何产生两个 begin_word 呢?
 
我倒是觉得如果格式固定的话,用个二维数组也可以解决问题
 
for i := 0 to AListSource.Count - 1 do begin SListSrcWord.CommaText := AListSource
if SListSrcWord[0] <> Sect then begin AListTarget.Add(SListTargetWord.CommaText)
//这句错误了更正为:AListTarget.Add(begin_word+':'+SListTargetWord.CommaText);另外: btn1click事件里面增加一句: Mmo2.Clear
...
 
修改你的方法:觉得你搞的复杂了procedure FormatStrs(AListSource, AListTarget: TStrings);var SListSrcWord: TStringList
Sect: string
i : Integer
Strtemp:String;begin Sect := ''
SListSrcWord := TStringList.Create
AListTarget.BeginUpdate
AListTarget.Clear
try Strtemp := ''
for i := 0 to AListSource.Count - 1 do begin SListSrcWord.CommaText := AListSource
if SListSrcWord[0] <> Sect then begin if Strtemp <> EmptyStr then AListTarget.Add(Strtemp)
Sect := SListSrcWord[0]
Strtemp := Sect+':'+ Format('%s%s%s', [SListSrcWord[2], SListSrcWord[3], SListSrcWord[4]])+','
end else begin Strtemp := Strtemp+ Format('%s%s%s', [SListSrcWord[2], SListSrcWord[3], SListSrcWord[4]])+','
end
end
if Strtemp <> EmptyStr then AListTarget.Add(Strtemp)
finally SListSrcWord.Free
end
AListTarget.EndUpdate;end;处理结果为:10BET:1,0,1,1,wBET:3,3,0,3,Astrabet:1,1,0,0,
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
700
SUNSTONE的Delphi笔记
S
S
回复
0
查看
694
SUNSTONE的Delphi笔记
S
I
回复
0
查看
583
import
I
I
回复
0
查看
639
import
I
后退
顶部