//一个比较傻的方法 var i,j:integer; //...... begin s1:=''; j:=0; for i:=1 to Length(s) do begin if IsDelimiter(',',s,i) then inc(j); if j<3 then s1:=s1+s[i] else break; end; end;
先要找到第三个逗号的位置,可以循环比较 然后用LeftStr
直接用LeftStr肯定不行,并不知道字符串的实际长度。 function GetSubStr(strData: string): string; var strTmp: string; iCount, iPostion: Integer; bOver: Boolean; begin Result := ''; strTmp := strData; iCount := 0; bOver := False; while (iCount < 3) and (not bOver) do begin iPosition := Pos(',', strTmp); if iPosition = 0 then bOver := True else begin strTmp[iPosition] := '#'; iCount := iCount + 1; end end; if iCount = 3 then Result := Copy(strData, 1, iPosition); end;
RightStr(str,n):从右向左
Copy(str,index,n):从任意位置,截取n个字符
var
i,j:integer;
//......
begin
s1:='';
j:=0;
for i:=1 to Length(s) do
begin
if IsDelimiter(',',s,i) then inc(j);
if j<3 then
s1:=s1+s[i]
else break;
end;
end;
然后用LeftStr
function GetSubStr(strData: string): string;
var
strTmp: string;
iCount, iPostion: Integer;
bOver: Boolean;
begin
Result := '';
strTmp := strData;
iCount := 0;
bOver := False;
while (iCount < 3) and (not bOver) do
begin
iPosition := Pos(',', strTmp);
if iPosition = 0 then
bOver := True
else
begin
strTmp[iPosition] := '#';
iCount := iCount + 1;
end
end;
if iCount = 3 then
Result := Copy(strData, 1, iPosition);
end;