Delphi 中是否有类似 C 语言中 scanf() 功能的函数或实现(100分)

Q

qzhyt

Unregistered / Unconfirmed
GUEST, unregistred user!
要读取以下一串以:分割的字符串中的数据
'James Bond : 360123345631: 123.00: 2351234: 8003'
用C语言可用以下代码实现:
char s[]="James Bond : 360123345631: 123.00: 2351234: 8003"

char s1[15], s2[20], s3[10];
int i;
float f;
sscanf(s,"%s:%s:%f:%s:%i", s1, s2, &f, s3, &i);
不知Delphi中是否有比较简单的类似实现
 
procedure TForm1.Button1Click(Sender: TObject);
var
s : string;
l_lst_Str: TStringList;
s1, s2, s3: string;
i : Integer;
f : Double;
begin
s := 'James Bond : 360123345631: 123.00: 2351234: 8003';

l_lst_Str := TStringList.Create;
try
l_lst_Str.Text := StringReplace(s, ':', #13#10, [rfReplaceAll]);
s1 := l_lst_Str[0];
s2 := l_lst_Str[1];
f := StrToFloat(l_lst_Str[2]);
s3 := l_lst_Str[3];
i := StrToInt(l_lst_Str[4]);
finally
l_lst_Str.Free;
end;
end;
 
function Format(const Format: string
const Args: array of const): string
overload;
例如:
Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])
返回:
1234
 
svw0506的方法可以实现,谢谢。但不知是否有更简单一点的做法。
 
不好意思看错了题目
给大家添麻烦了
 
///分割一个字符串,其中分割的标志是ch
function SplitString(const source,ch:string):tstrings;

function SplitString(const source,ch:string):tstrings;
var
temp:string;
i:integer;
begin
result:=tstringlist.Create;
temp:=source;
i:=pos(ch,source);
while i<>0 do
begin
result.Add(copy(temp,0,i-1));
delete(temp,1,i);
i:=pos(ch,temp);
end;
result.Add(temp);
end;
 
多人接受答案了。
 
顶部