谁能帮我解释一下这个函数的作用啊
function TObjModel.GetSubString(Line: String; ValueCount: Integer): String;
var fP,eP:Integer; vn:Integer;
i:Integer;
function WordCount(Line:String):Integer;
var i :Integer;
begin
Line:=' '+Line;
Result:=0;
for i := 1 to Length(Line)-1 do
begin
if (Line[i]=' ') and (Line[i+1]<>' ') then
Result := Result+1;
end;
end;
begin
vn:=0;
fp:=1;
ep:=0;
Line:=Line+' ';
if ValueCount>0 then
for i := 2 to Length(Line)-1 do
begin
if (Line[i+1]<>' ')and(Line[i] = ' ') then
Inc(vn); //vn+1
if vn=ValueCount then
begin
fp := i+1;
Break;
end;
end;
if ValueCount<WordCount(Line)-1 then
for i := fp+1 to Length(Line) do
begin
if Line[i] = ' ' then
begin
ep:=i;
Break;
end;
end
else
ep := Length(Line);
Result := Copy(Line,fp,ep-fp);
end;
function TObjModel.GetSubString(Line: String; ValueCount: Integer): String;
var fP,eP:Integer; vn:Integer;
i:Integer;
function WordCount(Line:String):Integer;
var i :Integer;
begin
Line:=' '+Line;
Result:=0;
for i := 1 to Length(Line)-1 do
begin
if (Line[i]=' ') and (Line[i+1]<>' ') then
Result := Result+1;
end;
end;
begin
vn:=0;
fp:=1;
ep:=0;
Line:=Line+' ';
if ValueCount>0 then
for i := 2 to Length(Line)-1 do
begin
if (Line[i+1]<>' ')and(Line[i] = ' ') then
Inc(vn); //vn+1
if vn=ValueCount then
begin
fp := i+1;
Break;
end;
end;
if ValueCount<WordCount(Line)-1 then
for i := fp+1 to Length(Line) do
begin
if Line[i] = ' ' then
begin
ep:=i;
Break;
end;
end
else
ep := Length(Line);
Result := Copy(Line,fp,ep-fp);
end;
解决方案 »
- 添加代码的地方和技巧问题!
- 能否让软件里的窗体保持windows 经典样式的样子
- 在delphi中如何实现vb中的“WithEvents”截获com中的事件!
- 请问如何将已存储的QuickRep的.qrp打印文件导入到QuickRep中打印?
- 银河网络招聘delphi以及VC程序员数名
- 在高州的老妹是不是也在搞传销?!郁闷 兼 救命啊~~~~~~!(散分)
- TXT、DBF文件怎么连ADO?在线等待,急!!!解决马上给分
- Plug_in技术方面的问题(在线)
- 如何设置treeview控件的节点在选中状态下为一个图标,不选中状态下为另一个图标,就像WIN资源管理器。
- 用delphi做数据库应用,选什么后台数据库好?
- 如何拦载有dbgrideh的自动提交数据的功能
- 如何把一个目录里面的文件名存放到下拉列表中去
比如 Line := 'a b c';GetSubString(line,0) := 'a';
GetSubString(line,1) := 'a';
GetSubString(line,2) := 'a';如果ValueCount的值大于空格的数量,则返回整个字符串
GetSubString(line,1) := 'a';
GetSubString(line,2) := 'a'; ????能否说具体点啊
var i :Integer;
begin
Line:=' '+Line;
Result:=0;
for i := 1 to Length(Line)-1 do
begin
if (Line[i]=' ') and (Line[i+1] <>' ') then
Result := Result+1;
end;
end;
这个函数的作用是返回字符串line中字串的个数(字串是用空格间隔的),如果字符串line=' a b c d'则返回值为4
如果valuecount的值大于等于line子串个数减1,则返回值为字符串line;
如果valuecount的值小于line子串个数减1,则返回值就是line中第valuecount+1个子串,是去掉空格的。
function TForm1.GetSubString(Line: String; ValueCount: Integer): String;
var
i:Integer;
str:TStrings;
begin
str:=TStringList.Create;
str.Clear;
ExtractStrings([' '],[],PChar(line),str);
i:=str.Count;
if valuecount>i-1 then
begin
result:=Line;
Exit;
end;
result:=str.Strings[ValueCount];
end;
写错了,^_^
GetSubString(line,0) := 'a';
GetSubString(line,1) := 'b';
GetSubString(line,2) := 'c';
WordCount是判断实际个数的,这个方法写的太罗嗦,用lovelymelon 的方法,明白了意图,自己写也是很容易的