線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:964
推到 Plurk!
推到 Facebook!

3 Questions

尚未結案
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-18 19:24:42 IP:213.106.xxx.xxx 未訂閱
original code: function TForm1.fromstr(const cstring: string; const nlen: integer): string; begin if length(cstring) >= nlen then result:= copy(cstring,nlen,succ(length(cstring)-nlen)) else result:='' ; end; function TForm1.Parse(var s: string; const c: string): string; { Parse a string on a given delimitor destructively (s=Right Hand Side(RHS)), returning Left Hand Side(LHS) } var p: Integer; begin p := ansiPos(c,s); if p > 0 then begin Result := Copy(s,1,p-1);{ Left hand side } s := FromStr(s,p Length(c));{ Right hand side } end else begin Result := s; s := ''; end; end; I try to apply parse function: procedure TForm1.Button6Click(Sender: TObject); var i,j :string; begin i:=edit3.text ;{here type c in edit3} j:=edit4.text; {abcedfg in edit4} parse(i,j); showmessage(parse(i,j)); end; The result should return to 'ab' in LHS, 'defg' in RHS. but it returns '' in the showmessage dialog! Q1: why it return ''? Q2: how many results does parse function return? 1 or 2 and which (s or result)? Q3: what are the differences between pos function and antipos function? please show me example if possible. Hope somebody can help me out! thx!
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-19 00:50:57 IP:219.129.xxx.xxx 未訂閱
lhh您好:
1、看了下程式,我想应该在edit3处输入abcedfg,在edit4输入c才能正确的得出您所要的结果。原因:在i:=c,j:=abcedfg;看那句p := ansiPos(c,s);时,如果按原来的输入方式代入p:=ansipos(j,i)-->p:=ansipos(abcedfg,c),所以p始终返回0,也就只有返回您所看到的为空的结果;
2、如果按照您的上述代码,做了第一步的调整(edit3处输入abcedfg,在edit4输入c)之后得出的结果应该是"edfg"只返回定位字符的右边字符,因为代码中返回结果是showmessage(parse(i,j));parse(i,j)只返回一个值,也即fromstr function 的result,但如果您想返回定位字符的左右两边的值,可以改成这样子:
function TForm1.Parse(var s: string; const c: string): string;
var
  p: Integer;
begin
  p := ansiPos(c,s);
  if p > 0 then
    begin
      Result := Copy(s,1,p-1);{ Left hand side }
      showmessage(result);//新增一句,用于返回Parse函数的值
      s := FromStr(s,p Length(c));{ Right hand side }
    end
  else
    begin
      Result := s;
      s := '';
    end;
end;
可以试试看
 >夜已深,困~~~如有谬误,请见谅~

补充一下:如提问题时,有代码加入,请在代码前后加入<>(>)……您的代码……<>(/>),>()用<>[]取代
——行径窄处,留一步与人行——    發表人 - 
        
lhh
一般會員


發表:16
回覆:21
積分:7
註冊:2004-11-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-19 07:29:43 IP:81.153.xxx.xxx 未訂閱
thanx so much !deity
系統時間:2024-07-03 20:55:22
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!