function GetPosion(str : string): Integer; var I ,nCount: Integer; begin Result := 0; nCount := 0; for I := 1 to Length(str) do begin if str[I] = ';' then begin inc(nCount); if nCount = 3 then begin Result := I; exit; end; end; end; end;
x := Pos(';', s); //第一个 x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第二个 x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第三个当然,你要另加判断到底有没有3个。
确定有至少三个分号?那就简单了Result := ';';
这样试试 s := '1;2;3;4;5;'; p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)));
s := '1;2;3;4;5;'; p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)+1)+1);
delphi函數function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer; Returns the index value of a substring. 翻譯:取得子串substr在母串S從第Offset個字符開始第一次出現的位置 Description PosEx returns the index of SubStr in S, beginning the search at Offset. If Offset is 1 (default), PosEx is equivalent to Pos. PosEx returns 0 if SubStr is not found, if Offset is greater than the length of S, or if Offset is less than 1. 翻譯: PosEx 取得子串substr在母串S從第Offset個字符開始第一次出現的位置。 如果Offset=1(默認值),PosEx的功能與pos相同。 如果找不到子串、Offset<1、Offset>母串S的長度,則返回0。 注意 uses StrUtils;
你可以用正则表式库TRegexpr(第三方的)
var s: string; i: Integer; Count: Integer; Position: Integer; begin Count := 0; s := 'a;b;c;d';{ÀýÈçÕâÑùµÄ×Ö·û´®} for i := 1 to Length(s) do begin if s[i] = ';' then begin Inc(Count); end; if Count = 3 then begin Position := i; ShowMessage(IntToStr(i)); Break; end; end; end;
pos第二次,截断
pos第三次
pos
找到一个加i++
当i等于3时就找到了
function GetPosion(str : string): Integer;
var
I ,nCount: Integer;
begin
Result := 0;
nCount := 0;
for I := 1 to Length(str) do
begin
if str[I] = ';' then
begin
inc(nCount);
if nCount = 3 then
begin
Result := I;
exit;
end;
end;
end;
end;
x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第二个
x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第三个当然,你要另加判断到底有没有3个。
s := '1;2;3;4;5;';
p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)));
p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)+1)+1);
delphi函數function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;
Returns the index value of a substring.
翻譯:取得子串substr在母串S從第Offset個字符開始第一次出現的位置
Description
PosEx returns the index of SubStr in S, beginning the search at Offset.
If Offset is 1 (default), PosEx is equivalent to Pos.
PosEx returns 0 if SubStr is not found, if Offset is greater than the length of S, or if Offset is less than 1.
翻譯:
PosEx 取得子串substr在母串S從第Offset個字符開始第一次出現的位置。
如果Offset=1(默認值),PosEx的功能與pos相同。
如果找不到子串、Offset<1、Offset>母串S的長度,則返回0。
注意 uses StrUtils;
s: string;
i: Integer;
Count: Integer;
Position: Integer;
begin
Count := 0;
s := 'a;b;c;d';{ÀýÈçÕâÑùµÄ×Ö·û´®}
for i := 1 to Length(s) do
begin
if s[i] = ';' then
begin
Inc(Count); end;
if Count = 3 then
begin
Position := i;
ShowMessage(IntToStr(i));
Break;
end;
end;
end;