//想实现检查字符串,并截取正确的字符。如'c:\dd\'变成'c:\dd','c:\dd\\'变成
//'c:\dd',‘c:\dd'就不用管他直接输出,现在的问题就是当str:=c:\dd\\,则返回的为空值,这是怎么回事?
function TForm1.jiancha(str: string):string;
var n:integer;
s,s1,s2:string;
begin
n:=length(str);//从1开始
s1:=copy(str,n,1);//倒数第一个
s2:=copy(str,n-1,1);//倒数第二个
if s1<>'/' then
Result:=str
else if s2<>'/' then
begin
result:=copy(str,1,n-1);
end
else if s2='/' then
begin
s:=copy(str,1,n-1);
result:=jiancha(s);
end
end;
//'c:\dd',‘c:\dd'就不用管他直接输出,现在的问题就是当str:=c:\dd\\,则返回的为空值,这是怎么回事?
function TForm1.jiancha(str: string):string;
var n:integer;
s,s1,s2:string;
begin
n:=length(str);//从1开始
s1:=copy(str,n,1);//倒数第一个
s2:=copy(str,n-1,1);//倒数第二个
if s1<>'/' then
Result:=str
else if s2<>'/' then
begin
result:=copy(str,1,n-1);
end
else if s2='/' then
begin
s:=copy(str,1,n-1);
result:=jiancha(s);
end
end;
var n:integer;
s,s1,s2:string;
begin
n:=length(str);//从1开始
s1:=copy(str,n,1);//倒数第一个
// s2:=copy(str,n-1,1);//倒数第二个
if s1<>'/' then
Result:=str
// else if s2<>'/' then
// begin
// result:=copy(str,1,n-1);
// end
else if s1='/' then
begin
s:=copy(str,1,n-1);
result:=jiancha(s);//注意这里要用result否则不知道返回的为何物
end
end;注解掉的都是不必要的
var n:integer;
s,s1,s2:string;
begin
n:=length(str);//从1开始
s1:=copy(str,n,1);//倒数第一个
// s2:=copy(str,n-1,1);//倒数第二个
if s1<>'/' then
Result:=str
// else if s2<>'/' then
// begin
// result:=copy(str,1,n-1);
// end
else if s1='/' then
begin
s:=copy(str,1,n-1);
result:=jiancha(s);//注意这里要用result否则不知道返回的为何物
end
end;注解掉的都是不必要的
其实这很简单:procedure TForm1.jiancha(s:string);
var
I : integer;
begin
I := Length(s);
while s[I] = '\'do
begin
s := Copy(s,1,I-1);
Dec(I);
end;
result := s;
end;
var
strTemp : string;
begin
strTemp := copy(s, length(s) - 1, 1);
if (strTemp <> '\') then
begin
result := copy(s,0,length(s)- 1);
end
else
begin
s := copy(s, 0, length(s)- 1);
result := JianCha(s);
end;
end;
随手写得,你试试,你那个麻烦了点!还有就是string类型中的起始位置不太对,既然用了递归,就不要什么倒数第二了!
var
strTemp : string;
begin
strTemp := copy(s, length(s), 1);
if (strTemp <> '\') then
begin
result := copy(s,0,length(s));
end
else
begin
s := copy(s, 0, length(s) - 1);
result := JianCha(s);
end;
end;
不好意思,有点错!
不要自己写了!
//from
http://kingron.myetang.com首部 function IsPathDelimiter(const S: string; Index: Integer): Boolean; $[SysUtils.pas
功能 返回字符串S中指定位置Index上是否是一个路径分隔符
说明 IsPathDelimiter('C:\Windows', 3) = True
参考 const SysUtils.PathDelim;function SysUtils.ByteType
例子 CheckBox1.Checked := IsPathDelimiter(Edit1.Text, SpinEdit1.Value);
/////////////////////
首部 function ExcludeTrailingPathDelimiter(const S: string): string; $[SysUtils.pas
功能 返回排除最后路径分隔符
说明 最后一个字符不是路径分隔符则不变;否则减去最后的路径分隔符返回
参考 function SysUtils.IsPathDelimiter;function System.Length;function System.SetLength
例子 Edit1.Text := ExcludeTrailingPathDelimiter(Edit2.Text);