delphi 根据特殊符号字符获取字符串前或后的字符

function GetBefore(substr, str:string):string;
{©Drkb v.3(2007): www.drkb.ru, 
®Vit (Vitaly Nevzorov) - nevzorov@yahoo.com}
begin
if pos(substr,str)>0 then
  result:=copy(str,1,pos(substr,str)-1)
else
  result:='';
end;


function GetAfter(substr, str:string):string;
{©Drkb v.3(2007): www.drkb.ru, 
®Vit (Vitaly Nevzorov) - nevzorov@yahoo.com}
begin
if pos(substr,str)>0 then
  result:=copy(str,pos(substr,str)+length(substr),length(str))
else
  result:='';
end;
 

演示:

1) 
GetBefore('-', 'Total-2.00$') // 结果 "Total"

2) 
GetAfter('-', 'Total-2.00$') // 结果 "2.00$"

3) 
GetBefore('$',GetAfter('-', 'Total-2.00$ (общая сумма)')  // 结果 "2.00"

 

posted on 2019-08-26 17:31  癫狂编程  阅读(1271)  评论(0编辑  收藏  举报

导航

好的代码像粥一样,都是用时间熬出来的