將字串分成多行的問題 |
|
horjaer
一般會員 ![]() ![]() 發表:2 回覆:3 積分:1 註冊:2002-07-11 發送簡訊給我 |
|
領航天使
站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
引言: 一段長字串...... 每到一定長度就折行......但要判斷中文字.... 不知要怎麼做比較好耶....^^"可用WideString來判斷中文字 var s:string; ws:widestring; i:integer; begin ws:=s; for i:=1 to length(ws) do begin if length(ws[i])>=2 then // 中文 else // 英文 ; end; end; ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~ |
danchiou
一般會員 ![]() ![]() 發表:10 回覆:12 積分:4 註冊:2003-02-11 發送簡訊給我 |
|
poemkevin
初階會員 ![]() ![]() 發表:26 回覆:77 積分:30 註冊:2002-10-19 發送簡訊給我 |
寫看看的,小弟是假設您把中文字也當做一個字元來看待 procedure TForm1.Button1Click(Sender: TObject);
var s:string;
ws:widestring;
i,x,last:integer;
ln:integer; //長度
begin
s:='1234567890一二三四五六七八九十';
ws:=s;
last:=1; //最左方定位
x:=1; //參考折行
ln:=5; //長度
for i:=1 to length(ws) do
begin
if length(ws[i])>=2 then // 中文
inc(x)
else // 英文
inc(x); if (x >= ln) and ((x mod 2) = 0) then
begin
memo1.Lines.Add(copy(ws,last,x-1));
x:=1;
last:=i;
end;
end;
end;
|
syntax
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
|
cancer
高階會員 ![]() ![]() ![]() ![]() 發表:58 回覆:319 積分:190 註冊:2004-07-31 發送簡訊給我 |
您好,
使用以下我自創的函式,s 是要檢查的字串,index 是字元的位置,
傳回值 true 則表示應字元是中文字的第一個字元。
所以,如果傳回 true ,則 index 和 index 1 就組成一個中文字,
必須在 index 或 index 2 的位置開始截斷字串,
不能在 index 1 處截。 function TForm1.DetermineLeadingByte
( s : string; index : Integer) : Boolean;
var i : Integer;
begin
result := false;
if (s = '') or (s[index] < #128)
or (index < 1) or (index > Length(s)) then exit;
i := 1;
while i <= index do
begin
if i < index - 2 then
begin
if s[i] < #128 then i := i 1 else i := i 2;
end
else break;
end; if i = index - 2 then
begin if s[i] > #127 then result := true
else result := s[i 1] < #127;
end
else if i = index - 1 then result := s[i] < #127
else if i = index then result := true;
end;
|
BOSS
中階會員 ![]() ![]() ![]() 發表:70 回覆:79 積分:64 註冊:2006-11-01 發送簡訊給我 |
var
s:string;
ws:widestring;
i,x,last:integer;
ln:integer; //長度
begin
s:='1234567890一二三四五六七八九十時';
ws:=s;
last:=1; //最左方定位
x:=1; //參考折行
ln:=5; //長度
for i:=1 to length(ws) do
begin
if length(ws[i])>=2 then // 中文
inc(x)
else // 英文
inc(x);
if (x >= ln) and ((x mod 2) = 0) then
begin
memo1.Lines.Add(copy(ws,last,x-1));
x:=1;
last:=i 1;
end;
if i=length(ws) then
memo1.Lines.Add(copy(ws,last,x-1));//未達折行的長度
end;
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |