半码的时候,我用copy做字符串截取,因汉字占2字节,标点、数字占一字节,这样很容易把汉字取成乱码。所以我想把字符串转换成全码,再截取,这样标点和数字也都占2字节,就不会取错了。
delphi有这样的程序吗?
如果没有,哪位大哥能告诉我有什么好点儿的解决方案?procedure TForm1.Button3Click(Sender: TObject);
var
s,s1,s2 : widestring;
begin
s := '按,1时豆腐干环境看来';
s1 := copy(s,1,6);
s2 := copy(s,7,50);
showmessage(s1);
showmessage(s2);
end;
delphi有这样的程序吗?
如果没有,哪位大哥能告诉我有什么好点儿的解决方案?procedure TForm1.Button3Click(Sender: TObject);
var
s,s1,s2 : widestring;
begin
s := '按,1时豆腐干环境看来';
s1 := copy(s,1,6);
s2 := copy(s,7,50);
showmessage(s1);
showmessage(s2);
end;
比如:s := '按,1;(1占2位)
我想取s1为'按,1's2为'时豆腐干环境看来',怎样才知道该取5位还是6位?
Function GetWord(Var Str:String;Len:Integer):String;
Var
WordCount,Index,StrLen:Integer;
Begin
StrLen:=Length(Str);
WordCount:=0;
Index:=1;
While Index<=StrLen Do
Begin
//判断字数是否大于Len个字
If WordCount>=Len Then
Begin
Result:=Copy(Str,1,Index-1);
//获取字符串前Len个字
Str:=Copy(Str,Index,StrLen-Index+1);
//退出函数
Exit;
End;
//判断该字符是否是汉字还是普通字符
If Str[Index]>#127 Then
Index:=Index+2
Else
Index:=Index+1;
//字数递增1
Inc(WordCount);
End;
//判断字数是否小于等于Len个字同时索引小于字符串长度
If (WordCount<=Len) And (Index>=StrLen) Then
Begin
//返回原串
Result:=Str;
Str:='';
End;
End;
盼详解
var
isword:Boolean;
i:Integer;
begin
isword:=True;
Result:='';
for i:=1 to length(ws) do
begin
if ord(ws[i])>160 then
begin
if not isword then
begin
Result:=Result+' ';
isword:=True;
end;
Result:=Result+ws[i];
end
else
begin
isword:=not isword;
Result:=Result+ws[i];
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(MakeAllToWord('我爱Delphi5.0工具!'));
end;