0

我有一些 VB 源代码,想将其转换为 Delphi:

Do While Not EOF(textfile)
Line Input #textfile, Line

Dim retstring() As String         
retstring = Split(Line, Chr(32))
first  = retstring(0)
second = retstring(1)

我有一些类似于这些行的文本文件:

大家好
很高兴
再会

我在答案中尝试了一些源代码,但仍然遇到问题。我看到消息“hello all”和“nice to”,但实际上我想看到“hello”和“all”。

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;
4

5 回答 5

8

您可以使用TStringList类来拆分文本文件。

看这个例子:

program SplitTextFile;

{$APPTYPE CONSOLE}

uses
  Classes,
  SysUtils;

var
 Lines : TStringList;
 Split : TStringList;
 i     : Integer;
 j     : Integer;
begin
  Lines := TStringList.Create;
  try
    Lines.LoadFromFile('c:\software\demo.txt'); //assign the file name

    Split := TStringList.Create;
    try
      Split.Delimiter := ' '; // set the delimiter

      for i := 0 to Lines.Count - 1 do //iterate over the lines of the file
      begin
        Split.DelimitedText := Lines[i];
        for j := 0 to Split.Count - 1 do //iterate over the split elements
          Writeln(Split[j]);
      end;
    finally
      Split.Free;
    end;
  finally
    Lines.Free;
  end;

  Readln;
end.
于 2009-12-18T11:58:18.230 回答
4

在分隔符上拆分字符串的一种方法是使用 TStringlist:

var
  list : TStringList;

begin
  list := TStringList.Create;
  try
    list.Delimiter := #32;
    list.DelimitedText := 'abc def ghi';
    first := list[0];
    second := list[1];
    third := list[2];
  finally
    list.Free;
  end;
于 2009-12-18T11:51:01.490 回答
1

如果您只想加载文本文件以进行操作,则 Stringlist 很方便。请注意,这是来自记忆,未经测试!

procedure loadtext;
var
  vList: TStringList;
  vFirst, vSecond: String; 
  i: Integer;
begin
  vList := TStringList.Create;
  try
    vList.LoadFromFile('myfile.txt');

    for i := 0 to vList.Count-1 do
    begin
      vFirst  := copy(vList[i], 0, pos(vList[i], ''));
      vSecond := copy(vList[i], pos(vList[i], ''), 1000);
    end;
  finally
    FreeAndNil(vList);
  end;  
end;
于 2009-12-18T11:57:16.797 回答
1

从我的 Turbo Pascal 时代开始,我就有一个通用实用程序,它可以满足您的要求:

function NumStringParts(SourceStr,Delimiter:String):Integer;
var
  offset : integer;
  curnum : integer;
begin
  curnum := 1;
  offset := 1;
  while (offset <> 0) do
    begin
      Offset := Pos(Delimiter,SourceStr);
      if Offset <> 0 then
        begin
          Inc(CurNum);
            Delete(SourceStr,1,(Offset-1)+Length(Delimiter));
        end;
    end;
  result := CurNum;
end;

function GetStringPart(SourceStr,Delimiter:String;Num:Integer):string;
var
  offset : integer;
  CurNum : integer;
  CurPart : String;
begin
  CurNum := 1;
  Offset := 1;
  While (CurNum <= Num) and (Offset <> 0) do
    begin
      Offset := Pos(Delimiter,SourceStr);
      if Offset <> 0 then
        begin
          CurPart := Copy(SourceStr,1,Offset-1);
          Delete(SourceStr,1,(Offset-1)+Length(Delimiter));
          Inc(CurNum)
        end
      else
        CurPart := SourceStr;
    end;
  if CurNum >= Num then
    Result := CurPart
  else
    Result := '';
end;

对于您的具体情况,您可以执行以下操作:

var
  Data : tStringlist;
  iX,iY,iCnt : integer;
begin
  data := tStringlist.create;
  try
    data.loadFromFile( filename );
    for iX := 0 to Data.Count-1 do
    begin
      iCnt := NumStringParts(Data.Strings[ix],#32);
      for iY := 1 to iCnt do
        ShowMessage( GetStringPart(Data.Strings[ix],#32,iY) );
    end;
  finally
    data.free;
  end;
end;

这将打开一个文件filename,然后调用CallSomeFunction文本文件中的每个单词(由空格分隔)。

于 2009-12-18T17:07:09.877 回答
0

或者如果你真的想要一个数组,你可以试试这个方法:

(复制自http://www.delphi3000.com/articles/article_2616.asp?SK=

  TStringArray = array of string;


  function Split(const str: string;
                 const separator: string): TStringArray;
  // Returns an array with the parts of "str" separated by "separator"
  var
    i, n: integer;
    p, q, s: PChar;
  begin
    SetLength(Result, Occurs(str, separator)+1);
    p := PChar(str);
    s := PChar(separator);
    n := Length(separator);
    i := 0;
    repeat
      q := StrPos(p, s);
      if q = nil then q := StrScan(p, #0);
      SetString(Result[i], p, q - p);
      p := q + n;
      inc(i);
    until q^ = #0;
  end; 
于 2009-12-18T11:55:03.747 回答