这样吧,提供一个可以提取汉字,字母,数字的函数给你,今后当预到这种情况就不用copy来copy去了. 相关的函数:function Getchineinnumer(s:string;i:integer):String; var t:WideString; j:Integer; begin result:=''; if i=1 then begin {返回中文} t := WideString(s); for j := 1 to Length(t) do if ord(t[j]) > 255 then Result := Result + AnsiString(t[j]); end else begin for J :=1 to length(s) do begin if i=2 then begin {返回字母} if UpCase(s[j]) in ['A'..'Z'] then result := result + s[j]; end else begin {反回数字} if Ord(s[j]) in [Ord('0')..ord('9')] then result := result + s[j]; end; end; end; end; 调用例子: 前题,窗体中用两个EDIT控件,EDIT1做被提取的之用,EDIT2是显示最后结果:procedure TForm1.Button1Click(Sender: TObject); begin {以下要分清来用,只取其一,以免错乱} edit2.Text:=Getchineinnumer(edit1.Text,0);{提取数字} edit2.Text:=Getchineinnumer(edit1.Text,1);{提取汉字} edit2.Text:=Getchineinnumer(edit1.Text,2);{提取字母} end; 结束语: 函数也许写得不好,如想进一步优化请自行解解,有不足之处敬请原谅!
var str:string; begin str:='(2358)[中国]'; ExtractStrings(['(',')','[',']'],[],pchar(str),Memo1.Lines);
中国: Copy('(2358)[中国]', 8, 4)
方法一
1、pos '('的位置
2、pos ')'的位置
3、取出中间的数据2358
4、pos '['的位置
5、pos ']'的位置
6、取出中间的数据'中国'方法二
使用正则表达式
是的,数字和汉字的位数是不固定的,记得以前有一个函数可以实现,现在记不得了,求老师们一段代码
谢谢
要取出的字符串A在()中
要取出的字符串B在[]中
那么A := Copy(S, Pos('(', S) + 1, Pos(')', S) - Pos('(', S) - 1)
B := Copy(S, Pos('[', S) + 1, Pos(']', S) - Pos('[', S) - 1)
相关的函数:function Getchineinnumer(s:string;i:integer):String;
var
t:WideString;
j:Integer;
begin
result:='';
if i=1 then begin {返回中文}
t := WideString(s);
for j := 1 to Length(t) do
if ord(t[j]) > 255 then Result := Result + AnsiString(t[j]);
end else begin for J :=1 to length(s) do
begin
if i=2 then begin {返回字母}
if UpCase(s[j]) in ['A'..'Z'] then
result := result + s[j];
end else begin {反回数字}
if Ord(s[j]) in [Ord('0')..ord('9')] then
result := result + s[j];
end;
end;
end;
end;
调用例子:
前题,窗体中用两个EDIT控件,EDIT1做被提取的之用,EDIT2是显示最后结果:procedure TForm1.Button1Click(Sender: TObject);
begin
{以下要分清来用,只取其一,以免错乱}
edit2.Text:=Getchineinnumer(edit1.Text,0);{提取数字} edit2.Text:=Getchineinnumer(edit1.Text,1);{提取汉字} edit2.Text:=Getchineinnumer(edit1.Text,2);{提取字母}
end;
结束语:
函数也许写得不好,如想进一步优化请自行解解,有不足之处敬请原谅!
var
str:string;
begin
str:='(2358)[中国]';
ExtractStrings(['(',')','[',']'],[],pchar(str),Memo1.Lines);