稀里糊涂混成了个猩猩,散分以感谢帮助过我的网友们,谢谢你们啦!!!
另,我有一问题,有字符串包含有汉字、英文和数字,比如:关于某甲公司欠TCL北京分公司200万元的问题..... 现在需要把它转变成一个定宽的字符串列表,假如宽度定为14个汉字,则上面的字符串变为:关于某甲公司欠TCL北京分公司2
00万元的问题.....我用了以下方法:
...
Copy(WideString(S),1,14);
...
其结果为:关于某甲公司欠TCL北京分公
司200万元的问题.....不符合我的要求,请问各位如何解决。
谢谢先!
另,我有一问题,有字符串包含有汉字、英文和数字,比如:关于某甲公司欠TCL北京分公司200万元的问题..... 现在需要把它转变成一个定宽的字符串列表,假如宽度定为14个汉字,则上面的字符串变为:关于某甲公司欠TCL北京分公司2
00万元的问题.....我用了以下方法:
...
Copy(WideString(S),1,14);
...
其结果为:关于某甲公司欠TCL北京分公
司200万元的问题.....不符合我的要求,请问各位如何解决。
谢谢先!
var
i : integer;
W1, Cur : integer;
begin
if W <= 1 then W := 1;
W := W * 2; // 每个汉字占两个字符
i := 1;
Result := '';
Cur := 0;
while i <= Length(St) do
begin
if St[i] >= #128 // 是汉字或其它占两个字节的字符
then W1 := 2 else W1 := 1;
if Cur + W1 > W then
begin
Result := Result + #13#10; //换行
Cur := 0;
end;
inc(Cur, W1);
Result := Result + St[i];
if (W1 = 2) and (i < Length(St)) then Result := Result + St[i + 1];
inc(i, W1);
end;
end;调用cut('关于某甲公司欠TCL北京分公司200万元的问题.....', 14),返回:关于某甲公司欠TCL北京分公司20
0万元的问题.....
procedure StrToList(const S: string; Width: integer; var OutList: TStringList);
var
i, W1, Cur : integer;
TmpStr, Str : string;
begin
if Width <= 1 then Width := 1;
Width := Width * 2; // 每个汉字占两个字符
i := 1;
Cur := 0;
Str := S;
OutList.Clear;
TmpStr := '';
while i <= Length(Str) do
begin
if Str[i] >= #128 // 是汉字或其它占两个字节的字符
then W1 := 2 else W1 := 1;
if Cur + W1 > Width then
begin
OutList.Add(TmpStr);
TmpStr := '';
Cur := 0;
end;
inc(Cur, W1);
TmpStr := TmpStr + Str[i];
if (W1 = 2) and (i < Length(Str)) then
TmpStr := TmpStr + Str[i + 1];
inc(i, W1);
end;
OutList.Add(TmpStr);
end;使用时采用一下形式:
....
try
SS := TStringList.Create;
StrToList(Edit1.Text, 10, SS);
....
finally
SS.Free;
end;
....复杂过jellypillar(新型人类),但方便我后续处理。
一点建议:OutList用TStrings而不用TStringList,应用的范围会更广一些。但SS := TStringList.Create那句不能改。:)
last BreakChar position before MaxCol. Will not insert a break into an
embedded quoted string (both ''' and '"' supported) }function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet;
MaxCol: Integer): string; overload;
function WrapText(const Line: string; MaxCol: Integer = 45): string; overload;