delphi中之取出字符串中的数字或汉字

//之取出数字
var
s,tmpStr:string;
I:Integer;
begin
s:=edt1.Text;
for I=1 to Length(s) do
if s[I] in [‘0’…‘9’] then
tmpStr:=tmpStr+s[I];
ShowMessage(tmpStr);
end;
//之取出汉字
原理 :在windows中,中文和全角字符都占两个字节,
并且使用了ascii chart 2 (codes 128 - 255 )。
全角字符的第一个字节总是被置为163,
而第二个字节则是相同半角字符码加上128(不包括空格)。
如半角a为65,则全角a则是163(第一个字节)、 193 (第二个字节, 128 + 65 )。
而对于中文来讲,它的第一个字节被置为大于163,(
如 ’ 阿 ’ 为: 176 162 ),我们可以在检测到中文时不进行转换。

function TFrm_Main.FilterHz(s: string): string;
var

P: ^Byte;
Dest: string;
Index: Integer;
begin

Result := ‘’;

P := Pointer(@S[1]);
Dest := ‘’;
Index := 1;
while Index < Length(S) do
begin
if P^ >= 128 then //去除 0-127 字母与数据
begin
if P^ > 163 then // 是 汉字
begin
Dest := Dest + Chr(P^);
Inc(Index);
Inc§;
Dest := Dest + Chr(P^);
end
else
begin
Inc(Index);
Inc§;
end;
end;
Inc(Index);
Inc§;
end;

Result := Dest;
end;
————————————————
{从字符串中提取汉字的函数}
procedure StrToHanZiList(str: string; var List: TStringList);
var
p: PWideChar;
begin
if List = nil then List := TStringList.Create;
List.Clear;
{去除重复}
List.Sorted := True;
List.Duplicates := dupIgnore;
p := PWideChar(str);
while p^ <> #0 do
begin
case p^ of
#$4E00…#$9FA5: List.Add(p^);
end;
Inc§;
end;
end;
原文地址依次顺序
原文链接:https://blog.csdn.net/chinawcs/article/details/5991914
原文链接:https://zhidao.baidu.com/question/530483739.html
原文链接:http://www.delphitop.com/html/zifuchuan/1870.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值