急,怎么得到一个字符串中的第三个分号?

qin2006 2010-01-25 09:43:16
tr
...全文
210 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wliaoc 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ok1411 的回复:]
嗯,楼上的都可行
[/Quote]

同意
redlegend_126_com 2010-01-26
  • 打赏
  • 举报
回复
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;
柯本 2010-01-26
  • 打赏
  • 举报
回复
你可以用正则表式库TRegexpr(第三方的)
QQ5555044 2010-01-26
  • 打赏
  • 举报
回复

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;
CACACACACA 2010-01-26
  • 打赏
  • 举报
回复
s := '1;2;3;4;5;';
p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)+1)+1);
CACACACACA 2010-01-26
  • 打赏
  • 举报
回复
这样试试
s := '1;2;3;4;5;';
p := PosEx(';', s, PosEx(';', s, PosEx(';', s, 1)));
「已注销」 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 brightyang 的回复:]
var i:integer;
pos
找到一个加i++
当i等于3时就找到了
[/Quote]
估计最简单最直观的思路就是这个了
ahjoe 2010-01-25
  • 打赏
  • 举报
回复
确定有至少三个分号?

那就简单了

Result := ';';
  • 打赏
  • 举报
回复
x := Pos(';', s); //第一个
x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第二个
x := x + Pos(';', Copy(s, x+1, length(s)-x)); //第三个

当然,你要另加判断到底有没有3个。
kobaer 2010-01-25
  • 打赏
  • 举报
回复
ok1411 2010-01-25
  • 打赏
  • 举报
回复
嗯,楼上的都可行
kfcoffe 2010-01-25
  • 打赏
  • 举报
回复

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;
brightyang 2010-01-25
  • 打赏
  • 举报
回复
var i:integer;
pos
找到一个加i++
当i等于3时就找到了
shuangyan 2010-01-25
  • 打赏
  • 举报
回复
pos第一次,截断
pos第二次,截断
pos第三次
husion01 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mysterx 的回复:]
x := Pos(';', s);  //第一个
x := x + Pos(';', Copy(s, x+1, length(s)-x));  //第二个
x := x + Pos(';', Copy(s, x+1, length(s)-x));  //第三个

当然,你要另加判断到底有没有3个。
[/Quote]

加一条判断是否有三个分号,没有分号X返回0;
justin_wkf 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mysterx 的回复:]
x := Pos(';', s);  //第一个
x := x + Pos(';', Copy(s, x+1, length(s)-x));  //第二个
x := x + Pos(';', Copy(s, x+1, length(s)-x));  //第三个

当然,你要另加判断到底有没有3个。
[/Quote]
顶一下

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧