2009-12-18 36 views
0

其實我開的問題之前,字符串分割功能在Delphi

,但不能得到解答一下我究竟想要的,所以我想再問感謝所有

例如,我有一些文字文件名是「test.txt的」,和文字內容的內部看起來像

hello all 
good day 
happy is 

,我想修改下源來自「你好所有」我的意思是第一個索引來遍歷..

如果我點擊showmessage(第一),那麼我希望得到「你好」 test.txt文件裏面,

,如果點擊showmessage(第二),然後想「全部」和continuesly,

如果我再次點擊showmessage(第一)然後想要得到'好'和

再次點擊showmessage(第二)然後想要得到'天',我想要的是什麼。

在此先感謝!並感謝所有幫助過我的人!

procedure TForm1.BitBtn1Click(Sender: TObject); 
var 
    list : TStringList; 
    first, second, third: string; 
begin 
    list := TStringList.Create; 
    try 
    list.Delimiter := #32; 
    list.LoadFromFile('test.txt'); 
    first := list[0]; 
    second := list[1]; 
    ShowMessage(first); 
    ShowMessage(second); 
    finally 
    list.Free; 
    end; 
end; 

你好,你可以修改,例如像下面?

我想使用showmessage(第一個)和showmessage(兩個),如果非常感謝!

procedure TForm1.BitBtn1Click(Sender: TObject); 

var 
    theFileStuff : tstringList; 
    oneLine  : tStringList; 
    x,y   : integer; 
begin 
    theFileStuff  := tStringList.Create; 
    oneLine   := tStringList.create; 
    oneLine.Delimiter := #32; 
    theFileStuff.LoadFromFile('test.txt'); 
    for x := 0 to theFileStuff.count-1 do 
    begin 
      oneLine.DelimitedText := theFileStuff[x]; 
      for y := 0 to oneLine.count-1 
      do 
      //ShowMessage(oneLine[y]); 
      ShowMessage(first); 
      ShowMessage(second); 

    end; 
    oneLine.Free; 
    theFileStuff.Free; 
end; 
+2

@Paul:當然你可以想辦法改變「ShowMessage(oneline [y]);」變成「first:= oneline [0]; second:= oneline [1]; ShowMessage(first); ShowMessage(second);」。你需要嘗試自己弄清楚事情,不要指望每個人都爲你做所有事情。你永遠不會這樣學習。 –

+0

大家好,謝謝!我解決了,如果沒有其他人的幫助,也許我無法解決它,我真的很感激!再次感謝:) – paul

回答

3

試試這個

procedure TForm1.ShowFields(Sender: TObject); 
var 
    theFileStuff : tstringList; 
    oneLine  : tStringList; 
    x,y   : integer; 
begin 
    theFileStuff  := tStringList.Create; 
    oneLine   := tStringList.create; 
    oneLine.Delimiter := #32; 
    theFileStuff.LoadFromFile('fileName'); 
    for x := 0 to theFileStuff.count-1 do 
    begin 
      oneLine.DelimitedText := theFileStuff[x]; 
      for y := 0 to oneLine.count-1 
      do ShowMessage(oneLine[y]); 
    end; 
    oneLine.Free; 
    theFileStuff.Free; 
end; 

如果你知道有每行只有兩個項目,則可以將以下代碼:與

for y := 0 to oneLine.count-1 
do ShowMessage(oneLine[y]) 

ShowMessage(oneLine[0]); // First 
ShowMessage(oneLine[1]); // Second 

我的代碼更具通用性,可處理每行任意數量的項目

+0

感謝您的幫助!我被修改了一些你會檢查它?再次感謝 – paul

+1

@Paul:自己檢查一下。如果不這樣做,你無法學習。 –

+0

你好,再次感謝! :)我現在已經解決了! – paul

2

Delimiter屬性僅在使用DelimitedText屬性時具有含義。你將不得不使用2個獨立的TStringList對象來詢問你的內容,例如:

var 
    list, values : TStringList; 
    curListIdx, curValueIdx: Integer; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    curListIdx := -1; 
    curValueIdx := -1; 
    list := TStringList.Create; 
    values := TStringList.Create; 
    values.Delimiter := #32; 
end; 

procedure TForm1.FormDestroy(Sender: TObject); 
begin 
    list.Free; 
    values.Free; 
end; 

procedure TForm1.BitBtn1Click(Sender: TObject); 
var 
    S: String; 
begin 
    if curListIdx = -1 then 
    begin 
    list.LoadFromFile('test.txt'); 
    if list.Count = 0 then Exit; 
    curListIdx := 0; 
    end; 

    if curValueIdx = -1 then 
    begin 
    if curListIdx = list.Count then 
    begin 
     curListIdx := -1; 
     Exit; 
    end; 
    values.DelimitedText := list[curListIdx]; 
    Inc(curListIdx); 
    if values.Count = 0 then Exit; 
    curValueIdx := 0; 
    end; 

    S := values[curValueIdx]; 
    Inc(curValueIdx) 
    if curValueIdx = values.Count then curValueIdx := -1; 

    ShowMessage(S); 
end; 
+0

你好,感謝您的幫助!我想修改這樣的,你會幫我 – paul

+0

雷米,你可以做一個TStringList(SL)和一個字符串變量。 SL.LoadFromFile(),將SL.Text分配給該變量,然後在設置SL之後將該變量分配回SL.DelimitedText。分隔符(可能是SL.StrictDelimiter:= True)。 –