我有一些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 good day
我在答案中尝试了一些源代码,但仍然遇到问题。我看到消息'你好'和'很好',但实际上我想看到'你好'和'全部'。
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;
答案 0 :(得分: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.
答案 1 :(得分: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;
答案 2 :(得分: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;
答案 3 :(得分: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
。
答案 4 :(得分: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;