字符串处理的问题(20分)

  • 主题发起人 主题发起人 liuying1129
  • 开始时间 开始时间
L

liuying1129

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在souce字符串"ab cde"中找到以ab开头,cde结尾,中间有1个到n个空格的匹配字符串
 
//从后面开始截取的方法,只读出最后一个'ab' + n个空格 + 'cde'
function Getabcde(Source: string): string;
var
b, e, Len: Integer;
begin
Result := '';
b := 0;
e := 0;
Len := Length(Source);
while Len > 0 do
begin
if e > 0 then
begin
if (Source[Len] = ' ') then
Dec(Len)
else if (Copy(Source, Len - 1, 2) = 'ab') and (e - Len > 3) then
begin
b := Len - 1;
Result := Copy(Source, b, e - b + 1);
Break;
end
else e := 0;
end
else if Copy(Source, Len - 2, 3) = 'cde' then
begin
e := Len;
Dec(Len, 3);
end
else Dec(Len);
end;
end;
 
到是很好弄啊
if copy(source,1,2)='ab' and copy(source,length(source)-3,3)='cde'
and length(source)>5 and trim(copy(source,3,length(source)-5))='' then
中間的你再自已寫吧
 
先判断空格,然后找空格前和空格后的字符是否匹配,我的思路是这样
 
用正则表达式

(ab/s+cde)
 
二楼的方法好啊,不错~
不过为什么不用pos函数,要用copy()以后再比较哪?这样慢呀~
if (pos(source,'ab')=1) and (pos(source,'cde')=length(source)-3)
........二楼的......
还有
什么是正则表达式阿?我不知道。
 
多人接受答案了。
 
后退
顶部