全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:10757
推到 Plurk!
推到 Facebook!

如何在字串中找出特定字???

答題得分者是:syntax
TTS
初階會員


發表:72
回覆:66
積分:27
註冊:2003-05-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-11-17 23:02:39 IP:218.162.xxx.xxx 未訂閱
假使我要在某一字串中找出某個字 該如何做??? 如:在"物件導向討論區" 這個字串中 如何找到"向"這個字 -急!急!急!-
AB
高階會員


發表:166
回覆:262
積分:125
註冊:2003-08-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-11-17 23:16:38 IP:61.64.xxx.xxx 未訂閱
TTS 兄 您好: 提問的智慧 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=20571 文章當中切勿含有[急][求救]等無意義的字眼] 謝謝您的配合
huangeider
高階會員


發表:288
回覆:492
積分:231
註冊:2003-02-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-11-17 23:22:53 IP:61.231.xxx.xxx 未訂閱
用pos涵數 Delphi syntax: function Pos(Substr: string; S: string): Integer; Description In Delphi, Pos searches for a substring, Substr, in a string, S. Substr and S are string-type expressions. Pos searches for Substr within S and returns an integer value that is the index of the first character of Substr within S. Pos is case-sensitive. If Substr is not found, Pos returns zero. The PosEx function is similar to Pos, but provides additional features and can be used in C code. 例如 if pos('向','物件導向討論區')>0 then showmessage('找到了'); 「堅持」從洗馬桶做起 Aric
TTS
初階會員


發表:72
回覆:66
積分:27
註冊:2003-05-06

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-11-17 23:40:04 IP:218.162.xxx.xxx 未訂閱
請問huangeider 中文字不是兩碼嗎 那用pos回傳值會是什麼呢???
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-11-18 00:17:15 IP:63.84.xxx.xxx 未訂閱
您好!    Pos函數傳回的是一個數值, 如果回傳的數值大于0, 則表示在字串中找到子字串, 其接受String形態, 範例如下:
Var
  S : String;
  I : Integer;
begin
  S := ' Delphi is good ';
  While Pos(' ', S) > 0 do
    begin
      I := Pos(' ', S);
      S[I] := '_';
    end;
  ShowMessage(S);
end;
//以上程式碼會將' Delphi is good 'String字串改成'_Delphi_is_good_'
另外還有一個函數StrPos, 也可尋找子字串在字串中的位置, 但只接受PChar形態, 回傳PChar形態, 範例如下:
Const
  P1 : PChar = 'Delphi is good';
  P2 : PChar = 'is';
Var
  ReturnPoint : PChar;
begin
  ReturnPoint := StrPos(P1, P2);
    if ReturnPoint = nil then
      ShowMessage('找不到指定的字串' P2)
    else
      ShowMessage(P2 '出現在第' IntToStr(RetrunPoint-P1) '個字');
end; 
參考看看! ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
huangeider
高階會員


發表:288
回覆:492
積分:231
註冊:2003-02-26

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-11-18 00:44:37 IP:61.231.xxx.xxx 未訂閱
請問huangeider 中文字不是兩碼嗎 那用pos回傳值會是什麼呢??? 若只要確定有無此中文的話試試這個 if not(stringreplace(s,'向','',[rfReplaceAll])='物件導向討論區') then showmessage('找到了'); 「堅持」從洗馬桶做起 Aric
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

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