字串陣列中,跳脫> = <等符號 |
答題得分者是:jimmy_wei
|
goodjimmy
一般會員 ![]() ![]() 發表:20 回覆:26 積分:9 註冊:2004-02-19 發送簡訊給我 |
|
jimmy_wei
高階會員 ![]() ![]() ![]() ![]() 發表:9 回覆:176 積分:147 註冊:2003-08-28 發送簡訊給我 |
|
goodjimmy
一般會員 ![]() ![]() 發表:20 回覆:26 積分:9 註冊:2004-02-19 發送簡訊給我 |
|
jimmy_wei
高階會員 ![]() ![]() ![]() ![]() 發表:9 回覆:176 積分:147 註冊:2003-08-28 發送簡訊給我 |
|
jeff377
初階會員 ![]() ![]() 發表:9 回覆:60 積分:33 註冊:2004-08-10 發送簡訊給我 |
|
malanlk
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:20 回覆:694 積分:577 註冊:2004-04-19 發送簡訊給我 |
const
//DEFAULT_DELIMITERS = [' ', #9, #10, #13];
DEFAULT_DELIMITERS = [' ', '>', '<', '='];
type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; function GetToken(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string; function GetToken2(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var sStr: String; begin sStr := '>=79.8933 <89.70'; ShowMessage(GetToken(sStr,0)); // 全部字串 ShowMessage(GetToken(sStr,1)); // 79.8933 ShowMessage(GetToken(sStr,2)); // 89.70 ShowMessage(GetToken2(sStr,0)); // 全部字串 ShowMessage(GetToken2(sStr,1)); // '>' 及 '=' 中間的空字串 ShowMessage(GetToken2(sStr,2)); // 79.8933 ShowMessage(GetToken2(sStr,3)); // ' ' 及 '<' 中間的空字串 ShowMessage(GetToken2(sStr,4)); // 89.70 end; function GetToken(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string; var I, W, head, tail: Integer; bInWord : Boolean; begin I := 1; W := 0; bInWord := False; head := 1; tail := Length(S); while (I <= Length(S)) and (W <= index) do begin if S[I] in Delimiters then begin if (W = index) and bInWord then tail := I - 1; bInWord := False; end else begin if not bInWord then begin bInWord := True; Inc(W); if W = index then head := I; end; end; Inc(I); end; if bTrail then tail := Length(S); if W >= index then Result := Copy(S, head, tail - head 1) else Result := ''; end; function GetToken2(const S: string; index: Integer; bTrail: Boolean = False; Delimiters: TSysCharSet = DEFAULT_DELIMITERS): string; var I, W, head, tail: Integer; bInWord : Boolean; bPreDelimiters : Boolean; bIsDelimiters : Boolean; begin I := 1; W := 0; bInWord := False; head := 1; tail := Length(S); bPreDelimiters := False; while (I <= Length(S)) and (W <= index) do begin bIsDelimiters := (S[I] in Delimiters); if bIsDelimiters and (bPreDelimiters) then begin Inc(W); if W=index then begin head := I-1; tail := I-2; break; end; end else begin if bIsDelimiters then begin if (W = index) and bInWord then tail := I - 1; bInWord := False; end else begin if not bInWord then begin bInWord := True; Inc(W); if W = index then head := I; end; end; end; bPreDelimiters := (S[I] in Delimiters); Inc(I); end; if bTrail then tail := Length(S); if W >= index then Result := Copy(S, head, tail - head 1) else Result := ''; end;用用看 GetToken: 會跳脫分隔字元取出元素 GetToken2: 會依分隔字元(DEFAULT_DELIMITERS)設定取出字串包括空字串 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |